0

I'm using two kinds of validation: Client Side and Server Side on a Blazor Project.

Client side is using DataAnnotations, as usual and DataAnnotationsValidator and is working just fine.

Server Side is using this custom server side validation component:

public sealed class ServerSideValidator : ComponentBase
{
    private ValidationMessageStore _messageStore;

    [CascadingParameter] 
    private EditContext CurrentEditContext { get; set; }

    protected override void OnInitialized()
    {
        if (CurrentEditContext == null)
        {
            throw new InvalidOperationException($"{nameof(ServerSideValidator)} requires a cascading " +
                                                $"parameter of type {nameof(EditContext)}. For example, you can use {nameof(ServerSideValidator)} " +
                                                $"inside an {nameof(EditForm)}.");
        }

        _messageStore = new ValidationMessageStore(CurrentEditContext);
        CurrentEditContext.OnValidationRequested += (s, e) => _messageStore.Clear();
        CurrentEditContext.OnFieldChanged += (s, e) => _messageStore.Clear(e.FieldIdentifier);
    }

    public void DisplayErrors(Dictionary<string, List<string>> errors)
    {
        foreach (var (elementId, errorsForElement) in errors)
        {
            _messageStore.Add(CurrentEditContext.Field(elementId), errorsForElement);
        }
        CurrentEditContext.NotifyValidationStateChanged();
    }
}

And it's also working fine for "direct" properties of the model.

<ValidationMessage For="@(() => model.Property)"/>

Works great. Textbox is red rounded if it's invalid, after the server validation.

Problem is that properties of child model object are being validated (model is set as invalid) and are displayed on ValidationSummary, but the invalid field is not being marked as that.

<ValidationMessage For="@(() => model.Child.Property )"/>

So this is partially working.

When I'm server side validating the attribute, I'm populating the expected list:

IDictionary<string, List<string>> validationErrors

For direct childs (which works) I'm doing:

validationErrors.Add("fieldName", new List {"Is invalid...."});

For childs of model (which doesn't work) I'm doing:

validationErrors.Add("childName.fieldName", new List {"Is invalid...."});

As you can see, although child property is invalid, and form is invalid, jquery shows it as valid.

How do I need to name that property in order for the validator to display the errors?

enter image description here

Sparky
  • 98,165
  • 25
  • 199
  • 285
Erre Efe
  • 15,387
  • 10
  • 45
  • 77

1 Answers1

1

You need to use the ObjectGraphDataAnnotationsValidator (if you want to use a custom implementation you can find the sources online).

It's in preview but it works fine. Add this reference to your project:

<PackageReference Include="Microsoft.AspNetCore.Components.DataAnnotations.Validation" Version="3.2.0-rc1.20223.4" />

and use it instead of DataAnnotationsValidator:

<EditForm EditContext="@editContext" OnSubmit="@OnSubmit">
  @* replace this => <DataAnnotationsValidator /> *@
  <ObjectGraphDataAnnotationsValidator />
  <ValidationSummary />
...
Nicola Biada
  • 2,325
  • 1
  • 8
  • 22