c# - Change style component when validation error occurs -


i need change field style when validator finds error. in view i've done that

<form action="@url.action("editar")" method="post" name="fcadastro" id="fcadastro" class="stdform" role="form">     <p>         <label>nome:</label>         <span class="field field-validation-error">             <input type="text" name="nome" id="nome" class="form-control" value="@model.nome" maxlength="50" />             @html.validationmessagefor(model => model.nome)          </span>          <small class="desc">informe o nome produto. tamanho máximo: 50 caracateres</small>     </p> </form>  @section scripts{     <script src="~/scripts/jquery.validate.js" defer></script>     <script src="~/scripts/jquery.validate.additional.methods.min.js" defer></script>     <script src="~/scripts/jquery.validate.unobtrusive.js" defer></script> } 

my viewmodel class has:

[required(errormessage = "campo obrigatório")] public string nome { get; set; } 

i have configured web.config with:

<appsettings>     <add key="clientvalidationenabled" value="true" />     <add key="unobtrusivejavascriptenabled" value="true" /> </appsettings> 

so, need apply error style , set focus field error, not show error message. 1 of solutions have tried override errorplacement funtion of unobtrusive:

settings.errorplacement = function (label, element) {     // call old handler can update html     olderrorplacement(label, element);      form.addclass('has-error');     element.parents('span').addclass('text-danger');     element.focus(); }; 

but without success.


Comments