Reputation: 1479
I have a form with 11 fields for the shipping address and 11 fields for the billing address plus a checkbox called "shipping address is the same as billing address". When the box is checked, I use jquery to call .hide() on the div containing the shipping address inputs. How do I then disable client- and server-side validation for the shipping address fields?
Upvotes: 4
Views: 591
Reputation: 1060
Try this solution for the RequiredIf attribute in MVC4 (or 3) is available at
http://cchitsiang.blogspot.com/2011/04/requiredif-conditional-validation-if.html
One can allow the user to submit an incomplete form or completely validated form based on the checkbox being checked. Make sure to change the following in the RequiredIfAttribute.cs
[AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
In reference to the link mentioned above,
For dropdownlists to be validated, Make sure to do the following in the model.cs file
Add the IsCompleted boolean variable at the bottom of all variables
[Display(Name = "Completed")]
public bool IsCompleted { get; set; }
for Dropdownlists to be validated pass the DBsets in a ViewBag within your controller, for example,
ViewBag.Relationships = db.Relationships;
In the view, add the dropdowns as follows
@Html.DropDownListFor(model => model.RelationshipId, new SelectList(ViewBag.Relationships, "RelationshipId", "RelationshipName"), "--- Select ---") <br />
@Html.ValidationMessageFor(model => model.RelationshipId)
Upvotes: 1
Reputation: 1355
Add a custom attribute such as [RequiredIf] as explained here.
Upvotes: 0
Reputation: 61
@Brad,
In MVC 2, one would need to use Foolproof validation, available in MVC Contrib, as it was out of the box model aware validation. It would then require for you to use RequiredIf Attribute, so that the shipping fields would be required only if the checkbox is checked. You can still use jquery to hide/show the fields.
In MVC 3, there is built in model aware validation, but i think someone else can fill me in here how to do what you need. Maybe there is RequiredIF attribute in the MVC itself.
If you need any more help, I am sure I can even provide more help and code examples.
Upvotes: 0