validation - Custom error message with DataAnnotationsExtensions -
i'm trying scott kirkland's dataannotationsextensions work mvc4 project. i'm having problems client side validation of email address. i've added emailaddress annotation error message, when enter invalid email address not custom error message, instead generic email error message "please enter valid recipientemail address.".
my class looks this:
public class nprequest { [displayname("telefonnummer som skal overdrages")] [required(errormessage = "angiv telefonnummeret som skal overdrages")] public string phonenumer { get; set; } [displayname("recipient email address")] [emailaddress(errormessage = "this custom error message")] [required(errormessage = "the recipient email address required")] public string recipientemail { get; set; } public recipienttypeenum recipienttype { get; set; } } and view:
---snippet begin---
<div class="editor-label"> @html.labelfor(model => model.phonenumer) </div> <div class="editor-field"> @html.editorfor(model => model.phonenumer) @html.validationmessagefor(model => model.phonenumer) </div> <div class="editor-label"> @html.labelfor(model => model.recipientemail) </div> <div class="editor-field"> @html.editorfor(model => model.recipientemail) @html.validationmessagefor(model => model.recipientemail) </div> <p> <input type="submit" value="create" /> </p> ---snippet end---
edit: when inspect html looks this:
<input class="text-box single-line input-validation-error" data-val="true" data-val-email="this custom error message" data-val-required="the recipient email address required" id="recipientemail" name="recipientemail" type="email" value=""> it seems custom error message put data-val-email attribute. under impression dataannotationextension automatically added custom error message modelstate , thereby adding field-validation-error span, showing mvc validation error.
is assumption wrong? should write own javascript, extracts custom error message attribute , injects field-validation-error span?
can see i'm doing wrong?
i ended using mix of system.componentmodel.dataannotations , dataannotationsextensions. found out out of time data annotations make client side validation. time no client side validation, when check if phone number correct length.
public class nprequest { [displayname("phone number")] [minlengthattribute(8, errormessage = "phone number must 8 digits")] [maxlengthattribute(8, errormessage = "phone number must 8 digits")] [digitsattribute(errormessage = "phone number must 8 digits")] [required(errormessage = "phone number required")] public string phonenumber { get; set; } [displayname("modtagers email adresse")] [emailaddressattribute(errormessage = "invalid email")] [required(errormessage = "email required")] public string recipientemail { get; set; } public recipienttypeenum recipienttype { get; set; } }
Comments
Post a Comment