I have a form with client side validation, and when an error input
is detected, a class attribute is changed of an input field; It is changed to include "input-validation-error" class.
I want to change this class, in order not to use it but instead use Bootstraps class "is-invalid".
I tried using ASP.NET Core's TagHelpers, but this has no effect;
I believe that this will not work as the helpers will only work if the "whole page" is loaded, it does not help with client side validation.
When I search in the .NET project one finds the css class defined in,
the "Unobtrusive validation support library for jQuery".
What is the best way to change this class?
Could CSS help by changing a class from one to the other? (overriding the original class, not sure if this is possible)
Or should one use JavaScript to reconfigure JQuery?
Here is my TagHelper, adding the helpers: validation-for,validation-error-class,validation-valid-class
The Form/Html...
<input type="email" asp-for="Email" id="inputEmail" class="form-control" placeholder="Email address" required
validation-for="Email" validation-error-class="is-invalid" validation-valid-class="is-valid"/>
<span class="small" asp-validation-for="Email"></span>
Here is a snippet of the code for my TagHelper.
[HtmlTargetElement("input", Attributes = "validation-for,validation-error-class,validation-valid-class")]
public class ValidationErrorClassTagHelper : TagHelper
{
[HtmlAttributeName("validation-for")]
public ModelExpression For { get; set; }
[HtmlAttributeName("validation-error-class")]
public string ErrorClass { get; set; }
[HtmlAttributeName("validation-valid-class")]
public string ValidClass { get; set; }
[HtmlAttributeNotBound]
[ViewContext]
public ViewContext ViewContext { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
output.RemoveClass(ErrorClass,HtmlEncoder.Default);
output.RemoveClass(ValidClass,HtmlEncoder.Default);
if (ViewContext.ViewData.ModelState.IsValid) {
output.AddClass(ValidClass,HtmlEncoder.Default);
} else
{
output.AddClass(ErrorClass,HtmlEncoder.Default);
}
}
}
New Approach not working 100%.
I have tried an alternative approach, by modifying the jQuery defaultOptions, changing the errorClass and the validClass.
Snippet of the Code found here on [https://github.com/brecons/jquery-validation-unobtrusive-bootstrap][gitHub]
function ($) {
if($.validator && $.validator.unobtrusive){
var defaultOptions = {
validClass: 'is-valid',
errorClass: 'is-invalid',
This works for the errorClass, but for me the validClass remains unchanged,
it remains to be named valid.