-1

Blazor validation not support individual field validation, its only validate all fields at a time in context. if i load my page at once in separated tab or stepper, validate controls partially not all fields. Is it possible? Please give me the solution. But i achieved in jquery by $('#ShippingInfo').validate().element('#ShippingAddress_StateID');

But blazor validate all the fields when press the submit button.

eg.,

I have a module like wizard using stepper component(it may be 4 steps). Here First step having some set of controls like customer details and 2nd step having some set of controls like get contact details and so on. All the controls are corresponding to one model class with annotations but validation by step by step by Next button using stepper component. How do i achieve this?

1 Answers1

2

Option 1

Divide your view model into several submodels "MySubmodel1", "MySubmodel2", etc. Then in your wizard-like form, have multiple <EditForm> elements, each specifying the corresponding submodel, ie. in the first <EditForm> set Model=@MySubmodel1, in the second <EditForm> set Model=@MySubmodel2, etc.

Option 2

This option is based on conditional validation of your view model. You will use just 1 viewmodel for the complete wizard-like form. Add a property "StepNumber" to your view model that will identify current step (step 1, step 2, ...), and define conditional validation of corresponding fields based on the actual step.

This can be achieved eg.

  • by using the custom Validation attributes like "RequiredIf" discussed eg. here on StackOverflow

or

  • by implementing the IValidatableObject (discussed eg. here) - in the Validate() method, write a code that will yield return ValidationResult for view model fields that do not have actual state as required by current step.

Finally, in your form, adjust current step when handling your Previous/Next step buttons.

rk72
  • 976
  • 1
  • 10
  • 15