If you want to compare two properties, look at the Compare attribute. Build
a new MVC 3 project and look at the account controller/model for reqistering a new user. The confirmPassword much match.
Well..I dont want to compare two properties..I want two properties to use the same Remote validation..
for instance like this:
Model:
public class BlogPostModel
{
[Required]
public Int64 ID { get; set; }
[Required]
[Remote("IsDateTimeFormat", "Validation", Fields = "Published")]
public DateTime Published { get; set; }
[Remote("IsDateTimeFormat", "Validation", Fields = "Updated")]
public DateTime Updated { get; set; }
[Required]
[DataType(DataType.Text)]
public string Title { get; set; }
[Required]
[DataType(DataType.MultilineText)]
public string HtmlContent { get; set; }
protected bool ValidateDateTime(object value)
{
if (((string)value).Length > 1)
return false;
return true;
}
}
The view:
[OutputCache(Location = OutputCacheLocation.None, NoStore = true)]
public class ValidationController : Controller
{
public JsonResult IsDateTimeFormat(string Published)
{
DateTime newdatetime;
bool success = DateTime.TryParse(Published, out newdatetime);
if (success)
return Json(true,JsonRequestBehavior.AllowGet);
else
return Json("The Date format you enterd is invalid!", JsonRequestBehavior.AllowGet);
}
}
In this case the called validation function only takes a parameter namned "Published", which meens that once Im validating the field with the name "Published" the validation will go through like it should and the parameter Published will get the value of the
field Published.
But if the field Updated tries to call this function then the parameter Published wont get set, since the only parameter that gets set/sent by the request is a parameter namned Updated..so is there any way to make this more generall?..so the parameters name
wont matter..
Another thing I also found out is that for some reason it seems that once Im entering something in the Published field(the textbox of the site) and then blur it(unfocus it) the validation function gets called for this field and all the other fields that
has this validation on them aswell..
Kind a hard to explain..but if you didnt get it at the above description of the problem then let me know and I will try to explain it abit more in details :)
Inx
Member
53 Points
217 Posts
Re: CustomValidation?
Nov 26, 2010 06:07 PM|LINK
Well..I dont want to compare two properties..I want two properties to use the same Remote validation..
for instance like this:
Model:
public class BlogPostModel { [Required] public Int64 ID { get; set; } [Required] [Remote("IsDateTimeFormat", "Validation", Fields = "Published")] public DateTime Published { get; set; } [Remote("IsDateTimeFormat", "Validation", Fields = "Updated")] public DateTime Updated { get; set; } [Required] [DataType(DataType.Text)] public string Title { get; set; } [Required] [DataType(DataType.MultilineText)] public string HtmlContent { get; set; } protected bool ValidateDateTime(object value) { if (((string)value).Length > 1) return false; return true; } }The view:
[OutputCache(Location = OutputCacheLocation.None, NoStore = true)] public class ValidationController : Controller { public JsonResult IsDateTimeFormat(string Published) { DateTime newdatetime; bool success = DateTime.TryParse(Published, out newdatetime); if (success) return Json(true,JsonRequestBehavior.AllowGet); else return Json("The Date format you enterd is invalid!", JsonRequestBehavior.AllowGet); } }In this case the called validation function only takes a parameter namned "Published", which meens that once Im validating the field with the name "Published" the validation will go through like it should and the parameter Published will get the value of the field Published.
But if the field Updated tries to call this function then the parameter Published wont get set, since the only parameter that gets set/sent by the request is a parameter namned Updated..so is there any way to make this more generall?..so the parameters name wont matter..
Another thing I also found out is that for some reason it seems that once Im entering something in the Published field(the textbox of the site) and then blur it(unfocus it) the validation function gets called for this field and all the other fields that has this validation on them aswell..
Kind a hard to explain..but if you didnt get it at the above description of the problem then let me know and I will try to explain it abit more in details :)