Live Chat Icon For mobile
Live Chat Icon

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

Platform: Blazor| Category: Forms and validation

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");
    }   
}

Please refer to this documentation for more form validations.

Share with

Share on twitter
Share on facebook
Share on linkedin

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.