I’m using Blazor Web App and have a component that uses EditForm and ValidationSummary. All works well, except I want to sort the error messages by the order specified in my model. Here’s my simpified-for-example model:
public class ContactModel
{
[Display(Order = 1)]
public int ContactID { get; set; }
[Required]
[Display(Order = 2)]
public string FirstName { get; set; }
[Display(Order = 3)]
public string MiddleName { get; set; }
[Required]
[Display(Order = 4)]
public string LastName { get; set; }
}
I’m adding a custom validation (example only) that if a middle name is entered, it must be at least 5 characters.
if (!String.IsNullOrEmpty(_model.MiddleName) && _model.MiddleName.Length < 5)
{
messageStore?.Add(() => _model.MiddleName, "Middle name must be at leaset 5 characters.");
}
_editContext.NotifyValidationStateChanged();
If I leave the first name blank and only enter two characters in the middle name, I get the two validation errors in the ValidationSummary… BUT… through some other testing I have found that the custom errors are displayed before that annotated errors. This makes the order
'Middle Name' must be at least 5 characters
'First Name' is required
I want to display the messages in the order defined in the model’s
[Display(Order = XX)]
attributes. How can I do this?
Thank you