And I whould like to enable validation for the Published property, and I want to check if the value of the property actually is a real DateTime-format..cause for now, it seems to take anything as long as it is a string..so is there any easy way to achive this,
I tried to use the [CustomValidation]-attribute but I just vouldnt figure out how that works..so if anyone could help me to explain the concept of that aswell I whould really appreciate it..and one more thing...if some how manage to use the CustomValidation-attribute,
whould that validation also "kick-in" on client side?..or whould that only be on the server-side?
public class DateValidatorAttribute : ValidationAttribute
{
public override bool IsValid(object value)
{
//this would not enforce "required" condition
if (value == null)
{
return true;
}
if (value.ToString() == "1/1/0001 12:00:00 AM") return false;
DateTime result;
bool status = DateTime.TryParse(value.ToString(), out result);
if (!status)
{
return false;
}
return true;
}
}
public class BlogPostModel
{
[DateValidator]
public DateTime Published { get; set; }
}
<asp:CompareValidatorID="cvDateTimeCheck"runat="server" ErrorMessage="Not a valid datetime data type."Type="Date"Operator="DataTypeCheck"></asp:CompareValidator>
Please note that we are discussing about MVC and NOT about classic ASP.NET. These kind of server sided controls DO NOT work in ASP.NET MVC.
<asp:CompareValidatorID="cvDateTimeCheck"runat="server" ErrorMessage="Not a valid datetime data type."Type="Date"Operator="DataTypeCheck"></asp:CompareValidator>
Please note that we are discussing about MVC and NOT about classic ASP.NET. These kind of server sided controls DO NOT work in ASP.NET MVC.
Yeah I kind of thought that aswell...but thanks for trying to help srinivas!
sachingusain
Will the code your wrote above(your custom-validation class) also "kick in" as a client side validation?..or will I have to build my own Javascript validator for that?
Will the code your wrote above(your custom-validation class) also "kick in" as a client side validation?..or will I have to build my own Javascript validator for that?
custom remote client validation is far simpler in MVC 3 than in MVC 2. You can read my MVC 2 tutorial
here. I can post the same sample in MVC 3 - the MVC 3 approach is trivial
Inx
Member
53 Points
217 Posts
Custom remote Validation in MVC 3
Nov 22, 2010 05:15 PM|LINK
Hi!
First of all..I have the following model:
And I whould like to enable validation for the Published property, and I want to check if the value of the property actually is a real DateTime-format..cause for now, it seems to take anything as long as it is a string..so is there any easy way to achive this, I tried to use the [CustomValidation]-attribute but I just vouldnt figure out how that works..so if anyone could help me to explain the concept of that aswell I whould really appreciate it..and one more thing...if some how manage to use the CustomValidation-attribute, whould that validation also "kick-in" on client side?..or whould that only be on the server-side?
Thanks in advance!
sachingusain
Star
8786 Points
1702 Posts
Re: CustomValidation?
Nov 22, 2010 06:29 PM|LINK
Here is sample code:
public class DateValidatorAttribute : ValidationAttribute { public override bool IsValid(object value) { //this would not enforce "required" condition if (value == null) { return true; } if (value.ToString() == "1/1/0001 12:00:00 AM") return false; DateTime result; bool status = DateTime.TryParse(value.ToString(), out result); if (!status) { return false; } return true; } } public class BlogPostModel { [DateValidator] public DateTime Published { get; set; } }You may modify it as required. You can follow this link for more information: http://haacked.com/archive/2009/11/19/aspnetmvc2-custom-validation.aspx
Thanks.
sachingusain
Star
8786 Points
1702 Posts
Re: CustomValidation?
Nov 22, 2010 06:30 PM|LINK
Please note that we are discussing about MVC and NOT about classic ASP.NET. These kind of server sided controls DO NOT work in ASP.NET MVC.
Thanks.
Inx
Member
53 Points
217 Posts
Re: CustomValidation?
Nov 22, 2010 07:27 PM|LINK
Yeah I kind of thought that aswell...but thanks for trying to help srinivas
!
sachingusain
Will the code your wrote above(your custom-validation class) also "kick in" as a client side validation?..or will I have to build my own Javascript validator for that?
sachingusain
Star
8786 Points
1702 Posts
Re: CustomValidation?
Nov 22, 2010 07:34 PM|LINK
I believe yes. Read this for details:
http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx
Thanks.
ricka6
All-Star
15088 Points
2277 Posts
Microsoft
Moderator
Re: CustomValidation?
Nov 22, 2010 09:29 PM|LINK
custom remote client validation is far simpler in MVC 3 than in MVC 2. You can read my MVC 2 tutorial here. I can post the same sample in MVC 3 - the MVC 3 approach is trivial
johannes.hie...
Member
441 Points
162 Posts
Re: CustomValidation?
Nov 23, 2010 07:53 AM|LINK
Hi Rick,
I also would love to see a sample for MVC3.
Best Regards,
Johannes
Inx
Member
53 Points
217 Posts
Re: CustomValidation?
Nov 23, 2010 11:48 AM|LINK
Yeah..the MVC 3 sample whould be great to see :), since thats what Im currently trying to learn :)
ricka6
All-Star
15088 Points
2277 Posts
Microsoft
Moderator
Re: CustomValidation?
Nov 23, 2010 09:32 PM|LINK
http://code.msdn.microsoft.com/aspnetmvcsamples/Release/ProjectReleases.aspx?ReleaseId=5114
feedback/suggestions welcome.
Sample requires MVC 3 RC or higher.
Inx
Member
53 Points
217 Posts
Re: CustomValidation?
Nov 23, 2010 10:28 PM|LINK
Sorry but this might be a stupid question.. I can't see any custombuilt validation attribute in the code-example..
Is it just to build the C#/VB class of my validation and then make sure to include those js-files:
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" /> <script src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.4.2.min.js"></script> <script src="http://ajax.microsoft.com/ajax/jquery.validate/1.7/jquery.validate.min.js"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>and then enable the following in the web.config:
<appSettings> <add key="enableSimpleMembership" value="true" /> <add key="ClientValidationEnabled" value="true"/> <add key="UnobtrusiveJavaScriptEnabled" value="true"/> </appSettings>and then I should be done for both server and client-side validation?..
Thanks for trying to help :) I might just have a hard time getting it all together..