Validation for the [Required] attributes are working fine. It's the DataType.EmailAddress that never gets checked at all. My ModelState.IsValid returns true even if I enter "x" as my email address. Haven't tried the other DataTypes, so I dunno if this is just
for the email or for all types.
Here it is. At a glance, I don't see the email validation at all.
<script type="text/javascript">
//<![CDATA[
if (!window.mvcClientValidationMetadata) { window.mvcClientValidationMetadata = []; }
window.mvcClientValidationMetadata.push({"Fields":[{"FieldName":"From","ReplaceValidationMessageContents":true,"ValidationMessageId":"From_validationMessage","ValidationRules":[{"ErrorMessage":"The From field is required.","ValidationParameters":{},"ValidationType":"required"}]},{"FieldName":"Subject","ReplaceValidationMessageContents":true,"ValidationMessageId":"Subject_validationMessage","ValidationRules":[{"ErrorMessage":"The Subject field is required.","ValidationParameters":{},"ValidationType":"required"}]},{"FieldName":"Message","ReplaceValidationMessageContents":true,"ValidationMessageId":"Message_validationMessage","ValidationRules":[{"ErrorMessage":"The Message field is required.","ValidationParameters":{},"ValidationType":"required"}]}],"FormId":"form0","ReplaceValidationSummary":true,"ValidationSummaryId":"validationSummary"});
//]]>
</script>
PS. I changed the model I pasted here earlier to make it shorter and easier to understand, but the only difference is, From == Email, and I took out Subject. But otherwise, they are identical with their attributes, etc
I also tried using "jquery-1.4.3.js" to see if the minimized version was buggy, as well as "jquery-1.4.1.min.js" and "jquery-1.4.1.js" because that's what was originally in the project (i updated the jquery with the latest from their site). Nothing still.
I tried changing the DataType to other stuff like currency, imageurl, date, etc... none work. I added a StringLength attribute just to make sure that the model was being validated against my modified copy and that shows up fine. It's really this DataType
attribute that simply fails outright.
Worldspawn, so you're using a custom validator for DataType annotations?
Sigh, I've had to resort to RegularExpression instead. Though I do hope that I just missed something with the DataType annotation. Hopefully someone can help.
Though I do hope that I just missed something with the DataType annotation. Hopefully someone can help.
No, DataType has no builtin validation
http://forums.asp.net/p/1560346/3852216.aspx
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
tomataus
Member
34 Points
28 Posts
DataType.EmailAddress not being Validated
Nov 10, 2010 11:00 PM|LINK
Hi,
I'm not sure if this problem is isolated in MVC2 or if it's isolated with the DataType.EmailAddress, but that's what I'm working on.
Anyway, I noticed that no validation is performed at all on my field marked with [DataType(DataType.EmailAddress)], both on client and server.
Here is the Model:
public class EmailModel { [Required] [DataType(DataType.EmailAddress)] public string Email {get; set;} [Required] public string Message {get; set;} }Here is the View:
<%= Html.ScriptInclude("jquery-1.4.3.min.js") %> <%= Html.ScriptInclude("MicrosoftAjax.js")%> <%= Html.ScriptInclude("MicrosoftMvcValidation.js") %> <% Html.EnableClientValidation(); %> <% using (Html.BeginForm()) {%> <!-- Email Address --> <%: Html.LabelFor(model => model.Email) %> <%: Html.TextBoxFor(model => model.Email)%> <%: Html.ValidationMessageFor(model => model.Email) %> <!-- Message --> <%: Html.LabelFor(model => model.Message) %> <%: Html.TextAreaFor(model => model.Message) %> <%: Html.ValidationMessageFor(model => model.Message) %> <% } %>Here are the Controller Actions:
public ActionResult SendEmail() { return View(new EmailModel()); } [HttpPost] public ActionResult SendEmail(EmailModel model) { if(ModelState.IsValid) { SendEmail(model); } return View(model); }Validation for the [Required] attributes are working fine. It's the DataType.EmailAddress that never gets checked at all. My ModelState.IsValid returns true even if I enter "x" as my email address. Haven't tried the other DataTypes, so I dunno if this is just for the email or for all types.
Any ideas?
Regards,
Tom
validation Data annotations mvc2 Email Address
worldspawn[]
Contributor
6081 Points
1336 Posts
Re: DataType.EmailAddress not being Validated
Nov 10, 2010 11:57 PM|LINK
Can you post the rendered validation init script (window.mvcClientValidationMetadata stuff)?
"Wise man say 'forgiveness is divine, but never pay full price for late pizza." - TMNT
software development
tomataus
Member
34 Points
28 Posts
Re: DataType.EmailAddress not being Validated
Nov 11, 2010 12:11 AM|LINK
Hi,
Here it is. At a glance, I don't see the email validation at all.
<script type="text/javascript"> //<![CDATA[ if (!window.mvcClientValidationMetadata) { window.mvcClientValidationMetadata = []; } window.mvcClientValidationMetadata.push({"Fields":[{"FieldName":"From","ReplaceValidationMessageContents":true,"ValidationMessageId":"From_validationMessage","ValidationRules":[{"ErrorMessage":"The From field is required.","ValidationParameters":{},"ValidationType":"required"}]},{"FieldName":"Subject","ReplaceValidationMessageContents":true,"ValidationMessageId":"Subject_validationMessage","ValidationRules":[{"ErrorMessage":"The Subject field is required.","ValidationParameters":{},"ValidationType":"required"}]},{"FieldName":"Message","ReplaceValidationMessageContents":true,"ValidationMessageId":"Message_validationMessage","ValidationRules":[{"ErrorMessage":"The Message field is required.","ValidationParameters":{},"ValidationType":"required"}]}],"FormId":"form0","ReplaceValidationSummary":true,"ValidationSummaryId":"validationSummary"}); //]]> </script>PS. I changed the model I pasted here earlier to make it shorter and easier to understand, but the only difference is, From == Email, and I took out Subject. But otherwise, they are identical with their attributes, etc
worldspawn[]
Contributor
6081 Points
1336 Posts
Re: DataType.EmailAddress not being Validated
Nov 11, 2010 12:23 AM|LINK
Could you add a call to ValidateFor for Email (or From). I'm sure ValidationMessageFor does the same thing though but give it a try.
I am using DataType.EmailAddress in much the same way and see it output correctly. It's very strange that you are seeing only some of the rules.
"Wise man say 'forgiveness is divine, but never pay full price for late pizza." - TMNT
software development
tomataus
Member
34 Points
28 Posts
Re: DataType.EmailAddress not being Validated
Nov 11, 2010 12:36 AM|LINK
Hi,
Still nothing.
I also tried using "jquery-1.4.3.js" to see if the minimized version was buggy, as well as "jquery-1.4.1.min.js" and "jquery-1.4.1.js" because that's what was originally in the project (i updated the jquery with the latest from their site). Nothing still.
Regards,
Tom
tomataus
Member
34 Points
28 Posts
Re: DataType.EmailAddress not being Validated
Nov 11, 2010 12:38 AM|LINK
Hi,
On a side note, there is a validation summary, so I know that ValidateFor didn't work.
And not to mention that it completely passes server validation.
Regards,
Tom
tomataus
Member
34 Points
28 Posts
Re: DataType.EmailAddress not being Validated
Nov 11, 2010 12:51 AM|LINK
Hi,
I tried changing the DataType to other stuff like currency, imageurl, date, etc... none work. I added a StringLength attribute just to make sure that the model was being validated against my modified copy and that shows up fine. It's really this DataType attribute that simply fails outright.
Regards,
Tom
worldspawn[]
Contributor
6081 Points
1336 Posts
Re: DataType.EmailAddress not being Validated
Nov 11, 2010 01:45 AM|LINK
Can you add this to your application_start code.Edit: actually that's not going to work DataTypeValidator is a custom class of mine. Anyway all the adapters should already be registered.
"Wise man say 'forgiveness is divine, but never pay full price for late pizza." - TMNT
software development
tomataus
Member
34 Points
28 Posts
Re: DataType.EmailAddress not being Validated
Nov 11, 2010 03:38 AM|LINK
Hi,
Worldspawn, so you're using a custom validator for DataType annotations?
Sigh, I've had to resort to RegularExpression instead. Though I do hope that I just missed something with the DataType annotation. Hopefully someone can help.
Regards,
Tom
imran_ku07
All-Star
45785 Points
7698 Posts
MVP
Re: DataType.EmailAddress not being Validated
Nov 11, 2010 04:28 AM|LINK
No, DataType has no builtin validation
http://forums.asp.net/p/1560346/3852216.aspx
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD