Blazored / FluentValidation

A library for using FluentValidation with Blazor
https://blazored.github.io/FluentValidation/
MIT License
577 stars 83 forks source link

Add Function to find out if a property is required based on Fluent validators #133

Open jdsmith39 opened 2 years ago

jdsmith39 commented 2 years ago

Is your feature request related to a problem? Please describe. I want to show an "*" (asterisks) with the label based on the fluent validators. So, I need a way of finding out if a specific property is currently required or not based on the fluent validators.

Describe the solution you'd like Perfect world: Have a function off of Blazored.FluentValidation.FluentValidationValidator that takes a property selecting expression, System.Linq.Expressions.Expression<Func>, as a parameter (need support for nested objects) and returns true if the property is required. Ideal world: Have a function off of Blazored.FluentValidation.FluentValidationValidator that takes a property name string (need to support nested objects) as a parameter and return true if the property is required. If there is something better than above, I'm good with that, too! Just need a reasonably easy way of finding out if a property is currently required or not. Of course, being required or not, can change on the fly based on the "when" conditions of the fluent validators.

Describe alternatives you've considered I'm currently using separate logic for showing/hiding the "" as needed. This means I have duplicated logic for required or not. Once in the fluent validators and once in the UI.
i.e. For DataAnnotationValidation, I put in an "
" when I see a Required attribute on the property.

Sharaf-Mansour commented 2 years ago

If I understand correctly you want something like this ? image Live demo: https://orange-plant-0c1e63203.azurestaticapps.net/

jdsmith39 commented 2 years ago

Looks like it may be doing what I'm looking for. I need to be able to do it dynamically. I change a project status and then the required fields change and I want field label asterisks to show up without duplicating the logic.

Sharaf-Mansour commented 2 years ago

@jdsmith39 In blazor you use the built in component

  <ValidationMessage For="@(()=> ...)" />

As for CSS style for input - I used

 editContext?.SetFieldCssClassProvider(new CustomFieldClassProvider());

CustomFieldClassProvider code

   public override string GetFieldCssClass(
                                      EditContext editContext,
                                      in FieldIdentifier fieldIdentifier)
        => !editContext.GetValidationMessages(fieldIdentifier).Any() ? 
               editContext.IsModified(fieldIdentifier) ?
                "is-valid" : "needs-validation" : "is-invalid";

This adds the green style when valid and red when invalid along with the SVG. You can create your custom CSS classes to add the * something like

.invalid{
content: '*';
color: red;
}

or even you can track it in the razor like.

<label> UserName @(()=> Model.UserName is null ? '*' : ' ' ) </label>

Wrap it in a component if you want. There is a lot of ways to do it. I get it, It came out of the box in the very old WinForm but this has nothing to do with this library, It is more of how Blazor is made... You can get the * from MVC with the normal Fluent Validation Library as it will replace the data annotation style of validation. Everything is customizable and flexible in Blazor. Build your own component and style it your way. I hope this gives you a close idea or an answer to your request :)