Avrohom Yisroel
Avrohom Yisroel

Reputation: 9460

How do I find out which properties had validation errors in a Blazor EditContext?

I have an EditForm, and am having some problems in that something is causing a validation error, but not something bound to a control on the form.

To clarify, I have a model for the form (some properties removed for clarity)...

public class RenewalViewModel {
    public int CurrentTermId { get; set; }
    public DateTime Start { get; set; }
    public DateTime End { get; set; }
    public ObservableCollection<CompanyOverview> AvailableCompanies { get; set; }
}

CurrentTermId is not shown on the form, but is needed to identity the term when the form is submitted. Start, End and some omitted properties are displayed on the form, can be edited by the user, ad are what is used when saving the data.

Along with those, there are a few properties, such as AvailableCompanies that hold data needed for validation, but are not used when the form is submitted. I need them on the model so the validator can see them (I'm using FluentValidation).

I suspect, but am not sure, that somehow, one of these associated properties (eg a CompanyOverview or something associated with that) has been changed, and is in an invalid state. However, I don't know how to find out what property of which object is causing the validation error in order to work out why it's happening.

In order to try and work out what was happening, I changed the form's submit handler to look like this...

    private void OnSubmitRenew(EditContext ec) {
      if (ec.ValidateObjectTree()) {
        // Save the data
      } else {
        Debug.WriteLine("Invalid data: " + string.Join(". ", ec.GetValidationMessages()));
      }
    }

The problem is that this only shows the validation errors, not which properties caused them.

Is there a way of working out which properties caused the errors? I had a look in the EditContext, but can't see anything useful in there.

Thanks

Upvotes: 4

Views: 2094

Answers (1)

enet
enet

Reputation: 45646

in that something is causing a validation error

How do you know ? Were you notified about it ? If so, what is the notification message ?

Validation error are usually in the form: First name is required... BirthofDate is required...etc. So most often we know what has gone wrong.

I'd suggest you try this code to locate the source of the error:

@code
    {
        private EditContext EditContext;
        private RenewalViewModel Model = new RenewalViewModel();
     
        protected override void OnInitialized()
        {
            EditContext = new EditContext(Model);
            EditContext.OnFieldChanged += EditContext_OnFieldChanged;

            base.OnInitialized();
        }

          // Note: The OnFieldChanged event is raised for each field in the model
        private void EditContext_OnFieldChanged(object sender, 
                                                FieldChangedEventArgs e)
        {
            

           // Each time a field changes this code is executed. 
              EditContext.Validate() returns true if
            // validation succeeded; that is, all fields pass validation
            if (EditContext.Validate())
            {
                Console.WriteLine("Validation succeeded");
            }
            else
            {
                // This is the culprit
                Console.WriteLine(e.FieldIdentifier.FieldName);
            }

        }
}

Upvotes: 2

Related Questions