1

I am having trouble implementing validation in Blazor. I currently am able to validate via attributes for things such as valid Guid inputs or required fields etc.

What I am after is a way to validate that some user entered data is not present in a database. From what I have read this cannot be done via an attribute as it is not possible to get the database context.

Could someone point me in the right direction as to how to validate data such as this.

Many thanks,

asked Oct 15, 2022 at 16:26

1 Answer 1

3

You could do it with FluentValidation. With it you can create simple to complex validation rules for your models and you can also validate with database data as well.

Follow the steps below of an example to check for email address if already registered in database. Hopefully, this example will point you in the right direction.

Install:
The Accelist.FluentValidation.Blazor nuget will provide integration of fluentvalidation with blazor.

dotnet add package FluentValidation
dotnet add package Accelist.FluentValidation.Blazor

Register Validator
This will allow you inject DB context in your validator.

builder.Services.AddScoped<IValidator<FormModel>, FormModelValidator>();

Implementation
Add FluentValidation element inside your form. And create validator for your model with a rule for Email property to check value with database if it exists.

<EditForm Model="Form">
 <FluentValidator></FluentValidator>
 
 <div class="form-group">
 <label for="email">Email</label>
 <InputText id="email" type="email" class="form-control" @bind-Value="Form.Email"></InputText>
 <ValidationMessage For="() => Form.Email"></ValidationMessage>
 </div>
 <div class="form-group">
 <button type="submit" class="btn btn-primary">
 <i class="fas fa-chevron-up"></i>
 Submit
 </button>
 </div>
</EditForm>
@code {
 FormModel Form = new FormModel();
 
 public class FormModel
 {
 public string Email { set; get; }
 }
 public class FormModelValidator : AbstractValidator<FormModel>
 {
 readonly AppDbContext DB;
 readonly IServiceProvider SP;
 public FormModelValidator(AppDbContext db, IServiceProvider sp)
 {
 this.DB = db;
 this.SP = sp;
 RuleFor(Q => Q.Email).NotEmpty().EmailAddress().MaximumLength(255)
 .Must(BeUniqueEmail).WithMessage("Email address is already registered.");
 }
 bool BeUniqueEmail(string email)
 {
 var exist = DB.Account.Where(Q => Q.Email == email).Any();
 return (exist == false);
 }
 }
}
answered Oct 15, 2022 at 17:05
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you so much for the answer, the only thing I have against using third party libraries is it feels like something this simple should be easily done without resorting to these libraries
@sam This is an alternative way, you can check out this link. Hopefully, it helps you. learmoreseekmore.com/2021/01/…
this looks like just want i need, i'll check it out. Thanks so much!
Using FluentValidation for my current project, used it before with MVC. You can create your own validator per ViewModel for example, put them all in DC, and then you only need to create a validator for each viewmodel/form, put tags around your thing in the component and it just works. I highly recommend it.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.