Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
[Parameter, EditorRequired]
public RegistrationModel Model { get; set; } = default!;

private ValidationMessageStore? _messages;
private ValidationMessageStore? messages;

protected override void OnInitialized()
{
ArgumentNullException.ThrowIfNull(CurrentEditContext);
_messages = new ValidationMessageStore(CurrentEditContext);
messages = new ValidationMessageStore(CurrentEditContext);
CurrentEditContext.OnValidationRequested += OnValidationRequested;
CurrentEditContext.OnFieldChanged += OnFieldChanged;
}
Expand Down Expand Up @@ -46,15 +46,15 @@

private async Task CheckAsync(FieldIdentifier field, CancellationToken token)
{
_messages!.Clear(field);
messages!.Clear(field);

var available = await Http.GetFromJsonAsync<bool>(
$"api/usernames/available?value={Uri.EscapeDataString(Model.Username)}",
token);

if (!available)
{
_messages.Add(field, "The username is already taken.");
messages.Add(field, "The username is already taken.");
}

CurrentEditContext!.NotifyValidationStateChanged();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,30 @@
[CascadingParameter]
private EditContext? CurrentEditContext { get; set; }

private ValidationMessageStore _messages = default!;
private ValidationMessageStore messages = default!;

protected override void OnInitialized()
{
ArgumentNullException.ThrowIfNull(CurrentEditContext);

_messages = new(CurrentEditContext);
messages = new(CurrentEditContext);
CurrentEditContext.OnValidationRequested += HandleValidationRequested;
CurrentEditContext.OnFieldChanged += HandleFieldChanged;
}

public void DisplayErrors(IDictionary<string, string[]> errors)
{
foreach (var (fieldName, messages) in errors)
foreach (var (fieldName, fieldMessages) in errors)
{
_messages.Add(CurrentEditContext!.Field(fieldName), messages);
messages.Add(CurrentEditContext!.Field(fieldName), fieldMessages);
}

CurrentEditContext!.NotifyValidationStateChanged();
}

public void ClearErrors()
{
_messages.Clear();
messages.Clear();
CurrentEditContext!.NotifyValidationStateChanged();
}

Expand All @@ -37,7 +37,7 @@

private void HandleFieldChanged(object? sender, FieldChangedEventArgs e)
{
_messages.Clear(e.FieldIdentifier);
messages.Clear(e.FieldIdentifier);
CurrentEditContext!.NotifyValidationStateChanged();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<EditForm Model="Model" OnValidSubmit="Submit">
<DataAnnotationsValidator />
<CustomValidation @ref="_remoteErrors" />
<CustomValidation @ref="remoteErrors" />
<ValidationSummary />

<p>
Expand Down Expand Up @@ -46,26 +46,26 @@
<button id="submit" type="submit">Submit</button>
</EditForm>

@if (_accepted)
@if (accepted)
{
<p id="accepted" role="status">The server accepted the form.</p>
}

@code {
private StarshipModel Model { get; } = new();
private CustomValidation? _remoteErrors;
private bool _accepted;
private CustomValidation? remoteErrors;
private bool accepted;

private async Task Submit()
{
_accepted = false;
accepted = false;

using var response = await Http.PostAsJsonAsync(
"api/starships/validate", Model);

if (response.IsSuccessStatusCode)
{
_accepted = true;
accepted = true;
return;
}

Expand All @@ -76,7 +76,7 @@
?? throw new InvalidOperationException(
"The validation response didn't contain a response body.");

_remoteErrors!.DisplayErrors(problem.Errors);
remoteErrors!.DisplayErrors(problem.Errors);
return;
}

Expand Down
Loading