Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Blazor Client Side Validation

$
0
0

I am trying a pure client side validation without invoking any SignalR Connection in Blazor Web App.I am using this code where signalr is invoked due to Interactive Server type render. On removing it the page keeps reloading on submit: -

Command line: -

dotnet new blazor

Code: -

@page "/pure"@rendermode InteractiveWebAssembly@using System.ComponentModel.DataAnnotations<EditForm Model="_login" OnValidSubmit="HandleValidSubmit" FormName="LoginForm"><DataAnnotationsValidator /><ValidationSummary /><InputText @bind-Value="_login.UserName" /><InputText @bind-Value="_login.Password" type="password" /><button type="submit">Submit</button></EditForm>@code {    private Login _login = new Login();    private async Task HandleValidSubmit()    {        // Perform any async tasks here        Console.WriteLine($"Submit clicked - no reloading: User name is {_login.UserName} and password is {_login.Password}");        // Optional: Simulate async operation        await Task.Delay(100);    }    public class Login    {        [Required(ErrorMessage = "User Name is required")]        public string? UserName { get; set; }        [Required(ErrorMessage = "Password is required")]        public string? Password { get; set; }    }}

On Blazor Web Assembly this code works perfect with no SignalR connection invoked.Help me to integrate the same in the above code

Command line: -

dotnet new blazorwasm

Web Assembly Code: -

@page "/custom"@using System.ComponentModel.DataAnnotations<EditForm Model="_login" OnValidSubmit="Submit"><DataAnnotationsValidator /><ValidationSummary /><InputText @bind-Value="_login.UserName" /><InputText @bind-Value="_login.Password" type="password" /><button type="submit">Submit</button></EditForm>@code {    private Login _login = new Login();    private async Task Submit()    {        // Display in console or do some action to verify it's not reloading        Console.WriteLine($"User name is {_login.UserName} and password is {_login.Password}");    }    public class Login    {        [Required(ErrorMessage = "User Name is required")]        public string? UserName { get; set; }        [Required(ErrorMessage = "Password is required")]        public string? Password { get; set; }    }}

I tried wasm but need to integrate the same effect in web app.


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>