oqtane / oqtane.framework

CMS & Application Framework for Blazor & .NET MAUI
http://www.oqtane.org
MIT License
1.88k stars 543 forks source link

Validators should be used on forms #256

Closed PavelVsl closed 4 years ago

PavelVsl commented 4 years ago

We have to implement validation. Not necessary for MVP but think about it. simple one DataAnnotationsValidator or something like https://github.com/ryanelian/FluentValidation.Blazor I'm using it and it's easy.

            RuleFor(x=>x.Email).NotEmpty().EmailAddress().MaximumLength(255)
                .Must(BeUniqueEmail)
               .WithMessage("Adresa je již zaregistrovaná");

            RuleFor(q => q.UserName)
                .NotEmpty()
                .Must(BeUnique)
                .WithMessage("Uživatel s tímto jménem již existuje");

            RuleFor(q => q.Password)
                .NotEmpty();
                RuleFor(q => q.Compare)
                    .Equal(q => q.Password);
            RuleFor(q => q.Phone)
                .NotEmpty()
                .MinimumLength(9)
                .Matches("^[+]?[()/0-9. -]{9,}$")
                .WithMessage("Telefonní číslo není správně vyplněno");
mikecasas commented 4 years ago

I agree with the validation, but is there not a library that picks up the "rules" from my model using data annotations? I don't want to define the requirements on my my model and then again for the validation with rules. Maybe it's me, but I like seeing the validation logic on my model.

public class Customer  
{   
    [StringLength(maximumLength: 50  , MinimumLength = 10,  ErrorMessage = "The property {0} should have {1} maximum characters and {2} minimum characters")]  
    public string Name { get; set; }  

} 
PavelVsl commented 4 years ago

DataAnnotationValidator does this job. But it'd not as flexible as FluentValidator. And on DataAnotations is some minor problems with localization.

https://docs.microsoft.com/en-us/aspnet/core/blazor/forms-validation?view=aspnetcore-3.1

PavelVsl commented 4 years ago

Main problems of current implementation of forms in Oqtane:

        <EditForm Model="@Model" OnValidSubmit="@Save">
            <DataAnnotationsValidator/>
            <ValidationSummary/>
.... form body ........          
           <button type="submit" >Save changes</button>
        </EditForm>
PavelVsl commented 4 years ago

@sbwalker Why is not used models, validators and ms components in forms? Any reason? fields in component class as model is against all patterns how to make forms in blazor. And terrible possibilities to easy refactoring. I smell old .asp here. I'm renaming same fields again and again .....

    string name = "";
    List<Tenant> tenants;
    string tenant = "";
    List<Alias> aliases;
    string urls = "";
    int logofileid = -1;
    FileManager filemanager;
    string themetype;
    string layouttype;
    string containertype;

    string smtphost = "";
    string smtpport = "";
    string smtpssl = "";
    string smtpusername = "";
    string smtppassword = "";

    string createdby;
    DateTime createdon;
    string modifiedby;
    DateTime modifiedon;
    string deletedby;
    DateTime? deletedon;
    string isdeleted;
sbwalker commented 4 years ago

Honestly, development of Oqtane began when Blazor was in the early Previews - back in 2018. At that time Blazor concepts like EditForm and many other features did not exist. And the code examples for Blazor were very limited at that time so the goal of the initial razor components was simply to make them functional. And as time went on the goal was to get all the of the required features completed for MVP - rather than going back and refactoring all of the existing functionality which was already working.

ADefWebserver commented 4 years ago

Also the built-in EditForm has 'limitations', for example the person has to sometimes hit the submit button twice after correcting validation errors.

ADefWebserver commented 4 years ago

Also some built-in Blazor features do not work as expected when they are in the dynamically injected controls that Oqtane implements.

PavelVsl commented 4 years ago

Ok, thank you for explanation. Code is little bit ugly but working. Sorry for my strong statements yesterday. I will focus on clean-up server, help with MVP, and this can be task for future versions, not for now.

sbwalker commented 4 years ago

Module developers have the freedom to use EditForm, annotations, etc... in their own modules but that does mean these techniques need to be used in the core framework. For V1 the core framework will use the current approach. Closing this issue and can revisit in V.Next.