Opinions expressed in this blog are my own personal opinions and do not represent the opinions of my employer in any way.
Code and information in this blog should be used at your own risk. I take no responsibility for any problems that may arise by anyone using code or information in this blog. Again, you may only use the code or information on this blog at your own risk.
Which is working fine for the server side , Now i want to display the validation message at client side , So i have followed the above procedure which you mentioned .
I have created a custom class :
JSelectOptionRequired which overrides the GetClientValidationRules() method like this :
public override IEnumerable GetClientValidationRules()
{
var rule = new ModelClientValidationRule { ErrorMessage = ErrorMessage, ValidationType = “jselectoptionrequired” };
return new[] { rule };
}
now i have added the method to MicrosoftMvcJQueryValidation.js
jQuery.validator.addMethod(“jselectoptionrequired”, function(value, element) {
if (value == undefined || value == null)
return false;
if (value.toString().length == 0)
return false;
return true;
});
and the other JQuery file i have included to my View.
@Nikhil – I would do a few things to try and determine what is happening.
Check the variable that MVC renders: look at the source MVC is rendering and search for the following string “window.mvcClientValidationMetadata.push” you should see a javascript array containing all the validation rules including the one that you return from the GetClientValidationRules() method.
If your custom rules are there then if you have firebug I suggest you open it up and put a breakpoint on the first line inside the jQuery.validator.addMethod … line… And see if it is firing…
That is where i would start… You need some more information I think (Sorry if you’ve already done all this! Fill me in with the details if so!)
Yes!! We have all the validation rules in “window.mvcClientValidationMetadata.push” and Yes it is hitting inside the jQuery.validator.addMethod() , After tracing i found this issue :
Im declaring my dropDownLitst like this in my View Page :
now my Client Side validation working fine , But my server side validation stops working, Since im using the same View for both Add/Update i need the Value property to bind the selected list value.
[...] to VoteCustom Server and Client Side Required Validator in MVC 2 using jQuery.validate (3/21/2010)Sunday, March 21, 2010 from jwwishartUpdate: Added information on issues that I’ve still yet to [...]
@nikhiluppaluri sorry for not replying earlier. Are you using the Html.DropDownListFor helper method in the above code you’ve given. It’s a bit hard to see what exactly you are doing (maybe due to the WordPress altering your code.)
March 23, 2010 at 1:29 am
Mice article jwwishart !!!
Im a newbie to MVC , I have a problem with my Cutom Model Validation , Hope you will help me to sort out.
My Custom Model class have a property for DropDownList :
[SelectOptionRequired("OldPosition",ErrorMessage = "Please select your old position.")]
[DataType(DataType.Text)]
[DisplayName("Old Position")]
public SelectListItem OldPosition { get; set; }
I have a custom class named SelectOptionRequiredAttribute.cs which implements ValidationAttribute class for DropDownList “Required” validation.
i have overriden IsValid method like this :
public override bool IsValid(object value)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value);
if (properties.Find(“value”, true).GetValue(value) == null)
{
return false;
}
else
{
if (properties.Find(“value”, true).GetValue(value).ToString() == “”)
{
return false;
}
return true;
}
}
Which is working fine for the server side , Now i want to display the validation message at client side , So i have followed the above procedure which you mentioned .
I have created a custom class :
JSelectOptionRequired which overrides the GetClientValidationRules() method like this :
public override IEnumerable GetClientValidationRules()
{
var rule = new ModelClientValidationRule { ErrorMessage = ErrorMessage, ValidationType = “jselectoptionrequired” };
return new[] { rule };
}
now i have added the method to MicrosoftMvcJQueryValidation.js
jQuery.validator.addMethod(“jselectoptionrequired”, function(value, element) {
if (value == undefined || value == null)
return false;
if (value.toString().length == 0)
return false;
return true;
});
and the other JQuery file i have included to my View.
But Client Side Validation is not working.
Could please help me to sort out this issue ?
Thanks in Advance.
Nikhil.
March 23, 2010 at 11:44 am
@Nikhil – I would do a few things to try and determine what is happening.
Check the variable that MVC renders: look at the source MVC is rendering and search for the following string “window.mvcClientValidationMetadata.push” you should see a javascript array containing all the validation rules including the one that you return from the GetClientValidationRules() method.
If your custom rules are there then if you have firebug I suggest you open it up and put a breakpoint on the first line inside the jQuery.validator.addMethod … line… And see if it is firing…
That is where i would start… You need some more information I think (Sorry if you’ve already done all this! Fill me in with the details if so!)
Thanks
March 24, 2010 at 1:20 am
Thank you very much jwwishart.
Yes!! We have all the validation rules in “window.mvcClientValidationMetadata.push” and Yes it is hitting inside the jQuery.validator.addMethod() , After tracing i found this issue :
Im declaring my dropDownLitst like this in my View Page :
model.OldPosition.Value, GenericLists.positionStatusList, “–select–”)%>
model.OldPosition)%>
and i have updated like this (removed the Value property)
model.OldPosition, GenericLists.positionStatusList, “–select–”)%>
model.OldPosition)%>
now my Client Side validation working fine , But my server side validation stops working, Since im using the same View for both Add/Update i need the Value property to bind the selected list value.
Please let me know what is the issue.
Thanks,
Nikhil.
March 24, 2010 at 1:22 am
model.OldPosition, GenericLists.positionStatusList, “–select–”)%>
model.OldPosition)%>
March 29, 2010 at 2:56 pm
[...] to VoteCustom Server and Client Side Required Validator in MVC 2 using jQuery.validate (3/21/2010)Sunday, March 21, 2010 from jwwishartUpdate: Added information on issues that I’ve still yet to [...]
April 13, 2010 at 10:18 am
@nikhiluppaluri sorry for not replying earlier. Are you using the Html.DropDownListFor helper method in the above code you’ve given. It’s a bit hard to see what exactly you are doing (maybe due to the WordPress altering your code.)
Have you solve this issue yet?
Thanks
jwwishart