Blazor

Is there an inline class for form attributes validation in Blazor? How do you define the validations in Razor component files?

Is there an inline class for form attributes validation in Blazor? How do you define the validations in Razor component files?

You can define the form attribute validation in a Razor component inside the function to achieve this. Please do not forget to include the DataAnnotations in the Razor component file.

@using System.ComponentModel.DataAnnotations;

<EditForm Model="@model" OnValidSubmit="@SubmitHandler">
    <DataAnnotationsValidator />
    <ValidationSummary />

    <div class="form-group d-flex justify-content-between">
        <label class="col-form-label col-3" for="name">Name</label>
        <InputText @bind-Value="@model.Name" id="name" Class="form-control" />
    </div>
    
    <button type="submit" @onclick="@SubmitHandler" class="btn btn-primary">Submit</button>
</EditForm>
@code {
    public class ModelClass
    {
        [Required]
        public string Name { get; set; }
    }
    private ModelClass model = new ModelClass();
    private void SubmitHandler()
    {
        Console.WriteLine("Submit");
    }   
}

I would suggest you check the below link for better understanding:
https://docs.microsoft.com/en-us/aspnet/core/blazor/forms-validation?view=aspnetcore-3.1

Sumitra Kumari Shaw

An Engineering graduate who Enjoys building things that live on the internet.