Closed DamianEdwards closed 3 years ago
cc: @pranavkm ... any additional guidance? ... any sneaky 😈 or hidden 🐉 for this scenario that I'll need to call out?
..... e.g., this probably can't be mixed (easily anyway) with existing validation approaches in the topic with a happy ending.
I was working on a simple Blazor app recently (two single-form pages) and I needed to add some fairly simple custom validation to a form (ensure at least one of a set of checkboxes was checked). I was surprised that the documentation guided me to create a custom validator component for such a basic scenario.
I suggest we update the "Business logic validation" section to include content that shows how to add basic custom validation to a form so that it correctly interacts with Blazor's built-in form validation logic, without having to encapsulate that logic in a separate component or validation attribute. This would be desirable in cases where the form's model is defined within the component hosting the form (either as members directly on the component or in a sub-class), rather than a model class that is separate for the purposes of reuse, etc.
The approach involves configuring an
EditForm
to use a declaredEditContext
andValidationMessageStore
instance, and then setting up a handler for theOnValidationRequested
event of theEditContext
, inside of which the custom validation logic is performed and the result of which is stored in theValidationMessageStore
instance.Example:
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.