Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

EditForm Validation With List of Model Rather Than Single Model

$
0
0

Single Model (Validates As Expected)

I have created an EditForm in Blazor where I was able to easily add validation for a single model where I added OnValidSubmit to the form with the DataAnnotationsValidator and ValidationSummary tags at the top:

<EditForm FormName="CriminalConvictions" Model="criminalConvictions" OnValidSubmit="SubmitMany"><DataAnnotationsValidator /><ValidationSummary />

This is the model declaration where is instantiates a new instance of the model:

[SupplyParameterFromForm]public CriminalConvictionModel? criminalConviction { get; set; }protected override async Task OnInitializedAsync(){    criminalConviction ??= new();}

Then I can add in the validation message under the field as well:

<InputTextArea @bind-Value="criminalConviction!.Offence" class="form-control" /><ValidationMessage For="@(() => criminalConviction!.Offence)" />

And in the model class I can then add a required tag to require a value for this field:

public class CriminalConvictionModel{    public int CriminalConvictionID { get; set; }    public DateTime? DateOfOffence { get; set; }    [Required(ErrorMessage = "Please record details of the offence")]    public string? Offence { get; set; }    ...}

This all works and validates the field as expected:Image of form running in browser with required field left blank

List of Model (Won't Validate)

However I now need to allow multiple items to be submitted so changed the model declaration to be a list of model objects like this:

public CriminalConvictionModel? criminalConviction { get; set; }[SupplyParameterFromForm]public List<CriminalConvictionModel>? criminalConvictions { get; set; }protected override async Task OnInitializedAsync(){    criminalConviction ??= new();    criminalConvictions = new List<CriminalConvictionModel>();    criminalConvictions?.Add(criminalConviction);}

Then I changed the submit function to save a list of models to the database which works and saves the records to the SQL database but the validation no longer works as this required field can now be left blank and it is not picked up and OnValidSubmit is being executed regardless of if the models in the list are valid or not.

If the EditForm model is set as the single criminalConviction then it validates the first set of elements but not subsequent ones but if I set it as the list I declared which is criminalConvictions then no validation occur and I can't work out what I need to change to make it work.

This is the function I added to add new models to the list of models to add a new set of form fields:

private void AddMoreConvictions() {    formSubmitted = false;    CriminalConvictionModel newConviction = new CriminalConvictionModel();    criminalConvictions = new List<CriminalConvictionModel>();    criminalConvictions?.Add(newConviction);}

So it all works and saves multiple records to the database but I just can't work out how to get validation working for List<Model> when it works as expected for Model and I can't work out what I am missing and also ideally it would be better if the validation summary could be per list of fields so it is clear which set has the missing field.

Other Approaches Attempted

The most obvious approach to this from looking online is to use ObjectGraphDataAnnotationsValidator, which seems to validate sub-elements and be exactly what I need, however it seems it was deprecated and removed with nothing to replace it.

I then attempted to write custom validation to cycle through the list objects checking each one for being empty but the form just still submitted.

Fluent validation is another option but it wouldn't work and led to an error saying No pending ValidationResult found. I'm not sure if I perhaps need a parent class that has a subset of the list of classes but then only the main subclass is in the database and is the only one I want to insert into using the form.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>