Home > Net >  Where is the OnValidSubmit validation occured in BlazorServer?(client or server or both)
Where is the OnValidSubmit validation occured in BlazorServer?(client or server or both)

Time:11-14

I use EditForm in the Blazor Server. The EditForm has a property named OnValidSubmit.
Suppose that I have used the Required attribute on a field in the model such as:

 [Required]
 public string Name { get; set; }

If the user disables JavaScript in the browser, is server-side validation also performed by OnValidSubmit?

There is the ModelState.IsValid In the asp.net mvc, Is there an alternative in Blazor? Considering the use of OnValidSubmit, is it really necessary to manually check the model validation again on the server side?

CodePudding user response:

If the user disables JavaScript in the browser, is server-side validation also performed by OnValidSubmit?

Yes, as long as no OnSubmit handler is registered on EditForm. Here's the relevant code from EditForm

    private async Task HandleSubmitAsync()
    {
        Debug.Assert(_editContext != null);

        if (OnSubmit.HasDelegate)
        {
            // When using OnSubmit, the developer takes control of the validation lifecycle
            await OnSubmit.InvokeAsync(_editContext);
        }
        else
        {
            // Otherwise, the system implicitly runs validation on form submission
            var isValid = _editContext.Validate(); // This will likely become ValidateAsync later

            if (isValid && OnValidSubmit.HasDelegate)
            {
                await OnValidSubmit.InvokeAsync(_editContext);
            }

            if (!isValid && OnInvalidSubmit.HasDelegate)
            {
                await OnInvalidSubmit.InvokeAsync(_editContext);
            }
        }
    }

In Blazor Server everything happens in the Hub Session on the server. In WASM it all happens in the WASM code running in the Browser.

EditForm has an EditContext instance (either passed to it directly or created from Model) which it cascades.

Editcontext has a ValidationMessageStore that maintains a list of valid messages.

EditContext has a Validate method that raises it's OnValidationRequested event. It doesn't do any validation itself.

Any validators with a registered handler get called, run their validation processes and log any messages to the central message store.

In the code below (taken from MS-Docs) DataAnnotationsValidator captures the cascaded EditContext from EditForm and registers a handler. When triggered it does it's business with the model attributes and logs any validation messages to the message store.

<EditForm Model="@starship" OnValidSubmit="@HandleValidSubmit">
    <DataAnnotationsValidator />

Consumer components, such as ValidationSummary capture the cascaded EditContext and read messages from the message store.

Input controls do the same thing to toggle their css based on state of their field.

All these consumers use the expression supplied in the For parameter to find the messages specify to the field supplied in the For expression.

CodePudding user response:

If the user disables JavaScript in the browser, is server-side validation also performed by OnValidSubmit?

Nyet

Actually, your Blazor Server App won't be loaded at all (This requires checking, but I'm almost sure of that)

There is the ModelState.IsValid In the asp.net mvc, Is there an alternative in Blazor?

Not yet, not yet...

Considering the use of OnValidSubmit, is it really necessary to manually check the model validation again on the server side?

Yes, of course...

The checks that you do on the Client side are purely for your front-end workflow/validation. You must always check the data that you get on your serverside no matter what. Relying on front-end check where every client could behave as it wishes (per your example, disable Javascript, or tweaking with the data sent over to your server-side piece) is not sufficient.
Julien

  • Related