Megabit / Blazorise

Blazorise is a component library built on top of Blazor with support for CSS frameworks like Bootstrap, Tailwind, Bulma, AntDesign, and Material.
https://blazorise.com/
Other
3.28k stars 530 forks source link

Add Data Anotations Validator support #120

Closed rogeramoros closed 4 years ago

rogeramoros commented 5 years ago

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.

ApocDev commented 5 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.

brettwinters commented 5 years ago

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...

stsrki commented 5 years ago

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.

brettwinters commented 5 years ago

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>
stsrki commented 4 years ago

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 Modelused for validation.

I'm just not sure if I'm also going to add Modelto the <Form> component so it can handle validation similar to the native <EditForm> that comes with Blazor.

devantler commented 4 years ago

I am a little confused reading this thread, so did this make it possible to use FluentValidation, or only Data Annotations for now?

stsrki commented 4 years ago

@niem94 You can use Data annotations. Look at the documentation: https://blazorise.com/docs/components/validation/#data-annotations