Solution 1: Use AddFluentValidationClientsideAdapters
You may use the FluentValidation.AspNetCore
and register the client-side validation by adding:
services.AddFluentValidationClientsideAdapters();
Solution 2: Use FormHelper
You may also use the FormHelper and, instead of using client-side validation you could instead execute your full server-side rules via AJAX.
Details
The FluentValidation GitHub readme says:
Clientside Validation
FluentValidation is a server-library and does not provide any
client-side validation directly. However, it can provide metadata
which can be applied to the generated HTML elements for use with a
client-side framework such as jQuery Validate in the same way that
ASP.NET's default validation attributes work.
Note that not all rules defined in FluentValidation will work with
ASP.NET's client-side validation. For example, any rules defined using
a condition (with When/Unless), custom validators, or calls to Must
will not run on the client side. Nor will any rules in a RuleSet
(although this can be changed - see below). The following validators
are supported on the client:
NotNull
/NotEmpty
Matches
(regex)
InclusiveBetween
(range)
CreditCard
Email
EqualTo
(cross-property equality comparison)
MaxLength
MinLength
Length
To enable clientside integration you need to install the
FluentValidation.AspNetCore package and call the
AddFluentValidationClientsideAdapters in your application startup:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddFluentValidationClientsideAdapters();
services.AddScoped<IValidator<Person>, PersonValidator>();
// etc
}
Note that the AddFluentValidationClientsideAdapters
method is only available in FluentValidation 11.1 and newer. In older versions,
you should use the AddFluentValidation
method which enables both
auto-validation and clientside adapters. If you only want clientside
adapters and don't want auto validation in 11.0 and older, you can
configure this by calling services.AddFluentValidation(config => config.AutomaticValidationEnabled = false)
Alternatively, instead of using client-side validation you could
instead execute your full server-side rules via AJAX using a library
such as FormHelper. This allows you to use the full power of
FluentValidation, while still having a responsive user experience.
Specifying a RuleSet for client-side messages
If you're using rulesets alongside ASP.NET MVC, then you'll notice
that by default FluentValidation will only generate client-side error
messages for rules not part of any ruleset. You can instead specify
that FluentValidation should generate clientside rules from a
particular ruleset by attributing your controller action with a
RuleSetForClientSideMessagesAttribute
:
[RuleSetForClientSideMessages("MyRuleset")]
public ActionResult Index(){ return View(new Person()); }
You can also use the SetRulesetForClientsideMessages extension method
within your controller action, which has the same affect:
public ActionResult Index()
{
ControllerContext.SetRulesetForClientsideMessages("MyRuleset");
return View(new Person());
}
You can force all rules to be used to generate client-side error
message by specifying a ruleset of "*".
Read more
Read more at: