Niestandardowy wzorzec RegularExpressionAttribute brak danych-val-regex dla walidacji po stronie klienta
Utworzono następujący niestandardowy atrybut RegularExpressionAttribute
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
public class AlphaNumericAttribute: RegularExpressionAttribute, IClientValidatable
{
public AlphaNumericAttribute()
: base("^[-A-Za-z0-9]+$")
{
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
yield return new ModelClientValidationRule { ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()), ValidationType = "alphanumeric" };
}
}
Pole w ViewModelu jest ozdobione moim atrybutem AlphaNumeric:
[AlphaNumeric(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = Resources.DriverLicenseNumber_RegexError_)]
public string DriverLicenseNumber { get; set; }
Pole jest zbudowane w widoku:
@using (Html.BeginForm("Index", "Application", FormMethod.Post, new { id = "applicationDataForm", autocomplete = "off" }))
{
@Html.LabelFor(m => m.DriverLicenseNumber)
@Html.ValidationMessageFor(m => m.DriverLicenseNumber)
@Html.TextBoxFor(m => m.DriverLicenseNumber)
}
To powinno dać właściwy"dane-" atrybuty walidacji na moim znaczniku wejściowym HTML. Jednak renderowany tag wygląda tak:
<input data-val="true" data-val-alphanumeric="Please enter a valid driver's license number." id="DriverLicenseNumber" name="DriverLicenseNumber" type="text" value="" maxlength="20" class="valid">
Wyraźnie nieobecni sądata-val-regex iwzorzec-danych-regexów atrybuty, które mają być renderowane.
Zbudowałem inne walidatory o dokładnie takiej samej strukturze i działają poprawnie, podobnie jak ta walidacja SSN, która obsługuje zamaskowane przestrzenie dla zamaskowanego wejścia przy użyciu maskowania jquery:
public class SsnAttribute : RegularExpressionAttribute, IClientValidatable
{
public SsnAttribute()
: base("^([0-9]{3}–[0-9]{2}–[0-9]{4})|([ ]{3}–[ ]{2}–[ ]{4})|([0-9]{9,9})$")
{
}
public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
{
yield return new ModelClientValidationRule { ErrorMessage = FormatErrorMessage(metadata.GetDisplayName()), ValidationType = "ssn" };
}
}
Z towarzyszącą aplikacją w ViewModel:
[Ssn(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = Resources.SocialSecurity_RegexError_)]
public new string SocialSecurityNumber { get; set; }
Pole jest zbudowane w widoku:
@using (Html.BeginForm("Index", "Application", FormMethod.Post, new { id = "applicationDataForm", autocomplete = "off" }))
{
@Html.LabelFor(m => m.SocialSecurityNumber)
@Html.ValidationMessageFor(m => m.SocialSecurityNumber)
@Html.TextBoxFor(m => m.SocialSecurityNumber)
}
Ten atrybut sprawdzania poprawności poprawnie renderuje atrybuty wzorca data-val-regex i data-val-regex:
<input class="SSNMask valid" data-val="true" data-val-regex="Please enter a valid social security number." data-val-regex-pattern="^([0-9]{3}–[0-9]{2}–[0-9]{4})|([ ]{3}–[ ]{2}–[ ]{4})|([0-9]{9,9})$" id="SocialSecurityNumber" name="SocialSecurityNumber" type="text" value="" maxlength="22">
Nie mogę zrozumieć, czego brakuje w atrybucie AlphaNumeric, który nie renderuje odpowiednich atrybutów HTML.