I’m trying to do login with blazor, but now the problem is that whenever I type in the data into inputtext fields I get validation error “this field is required” eventhough the information is entered. The model looks like this
public class UserModel
{
[Required(ErrorMessage = "Name is required")]
public string Name { get; set; }
[Required(ErrorMessage = "Surname is required")]
public string Surname { get; set; }
[Required(ErrorMessage = "Email is required")]
[EmailAddress(ErrorMessage = "Invalid email format")]
public string Email { get; set; }
[Required(ErrorMessage = "Password is required")]
[MinLength(6, ErrorMessage = "Password must be at least 6 characters long")]
public string PasswordHash { get; set; }
}
I doubt that this part is the problem, but dataAccess file that should contact database and insert data looks like this
public async Task<int> RegisterUser(UserModel user, string connectionString)
{
user.PasswordHash = BCrypt.Net.BCrypt.HashPassword(user.PasswordHash);
string sql = @"
INSERT INTO Users (Name, Surname, Email, PasswordHash)
VALUES (@Name, @Surname, @Email, @PasswordHash);
";
using (IDbConnection connection = new MySqlConnection(connectionString))
{
return await connection.ExecuteAsync(sql, user);
}
}
and the form which I try to insert data into looks like this
<EditForm EditContext="@editContext" OnValidSubmit="HandleRegister" FormName="registerForm">
<DataAnnotationsValidator />
<ValidationSummary />
<InputText @bind-Value="userModel.Name" Placeholder="Name" />
<InputText @bind-Value="userModel.Surname" Placeholder="Surname" />
<InputText @bind-Value="userModel.Email" Placeholder="Email" />
<InputText @bind-Value="userModel.PasswordHash" Placeholder="Password" type="password" />
<button type="submit">Register</button>
</EditForm>
The problem is like I said, whenever I try to submit input data, I get the verification error message, even if required data is inserted.
Here’s a Minimal Reproducible Example based on the code you’ve provided. It’s the home page from a Blazor Server / Global Interactivity project.
It works as it should, so you can’t be showing us all your code.
@page "/"
@using System.ComponentModel.DataAnnotations;
<PageTitle>Home</PageTitle>
<h1>Hello, world!</h1>
<EditForm EditContext="@editContext" OnValidSubmit="HandleRegister">
<DataAnnotationsValidator />
<ValidationSummary />
<InputText class="form-control mb-2" @bind-Value="userModel.Name" Placeholder="Name" />
<InputText class="form-control mb-2" @bind-Value="userModel.Surname" Placeholder="Surname" />
<InputText class="form-control mb-2" @bind-Value="userModel.Email" Placeholder="Email" />
<InputText class="form-control mb-2" @bind-Value="userModel.PasswordHash" Placeholder="Password" type="password" />
<div class="text-end">
<button class="btn btn-primary" type="submit">Register</button>
</div>
</EditForm>
@code {
private UserModel userModel = new();
private EditContext? editContext;
protected override void OnInitialized()
{
editContext = new(userModel);
base.OnInitialized();
}
private void HandleRegister()
{
Console.WriteLine($"Register");
}
public class UserModel
{
[Required(ErrorMessage = "Name is required")]
public string Name { get; set; }
[Required(ErrorMessage = "Surname is required")]
public string Surname { get; set; }
[Required(ErrorMessage = "Email is required")]
[EmailAddress(ErrorMessage = "Invalid email format")]
public string Email { get; set; }
[Required(ErrorMessage = "Password is required")]
[MinLength(6, ErrorMessage = "Password must be at least 6 characters long")]
public string PasswordHash { get; set; }
}
}