Closed rogeramoros closed 4 years ago
Honestly, I don't think anything special should be done aside from using the built in InputText which allows the Blazor devs to handle the bulk of the validation scenarios.
There's no reason to recreate the wheel, when it already exists. :)
For example; you can already add FluentValidation (https://chrissainty.com/using-fluentvalidation-for-forms-validation-in-razor-components/)
Doing any sort of wonky/hackish workaround to do your own validation is probably going to break sooner rather than later, and still not give developers the ability to insert their own validation providers.
I know validations are in your roadmap / backlog but my request is something like this:
Either subclass, or via common interface, or allow your users to extend (which they can do now actually) the Validations component:
<[Fluent/DataAnnotations/Blazorise/Custom?]Validations Validator="myFluentValidator" Mode="Auto/Manual">
<Validation [PropertyToValidate="Name"]>
<Field>
<!-- etc -->
<ValidationSucccess></ValidationSuccess>
<ValidationError></ValidationError>
</Field>
</Validation >
<!-- etc -->
<Validations>
For Fluent for Model = Auto
then each field is validated individually using the ValidationContext otherwise do a full model validation
Chris extended EditForm
which is another approach...
The plan is to clean and refactor Blazorise code in the versions 0.8>
and <=0.9
. Data annotations are going to be done in one of the versions in between. While I'm personally not a big fan of annotations because they are mostly fixed at compile-time I can see a use case for them.
@brettwinters The code for data-annotated validation will probably be something similar to your example.
yeah, I don't use them much either. I prefer FluentValidations (which was really the target of my request) but I just included AttributeValidation in the list since some people use them...
FYI : not sure if this causes problems under the hood, but I'm using FluentValidations with a helper extension method to validate like this:
<Validation Validator="@((e) => (e.Status, e.ErrorText ) = model.Validate(modelValidator, nameof(Model.Description)))">
<Field>
<FieldLabel>Description</FieldLabel>
<TextEdit @bind-Text="@model.Description">
<ValidationError></ValidationError>
</TextEdit>
</Field>
</Validation>
Just a quick update. I've managed to implement validation using data annotations. The only thing left now is to decide for the final API. Currently I'm using <Validations>
component to handle the Model
used for validation.
I'm just not sure if I'm also going to add Model
to the <Form>
component so it can handle validation similar to the native <EditForm>
that comes with Blazor.
I am a little confused reading this thread, so did this make it possible to use FluentValidation, or only Data Annotations for now?
@niem94 You can use Data annotations. Look at the documentation: https://blazorise.com/docs/components/validation/#data-annotations
Validation are supported in Blazor using data annotations. A set of built-in input components are available to receive and validate user input. For exemple <input> must be to accept validator <InputText> https://docs.microsoft.com/en-us/aspnet/core/blazor/forms-validation?view=aspnetcore-3.0
Creating a special Blazorise validator that could check for the annotations on supplied model.