4

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?

3 Answers 3

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

@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.

1 Comment

I've gotten around the problem by using JavaScript to put the values from the billing address fields into the shipping address fields, but I didn't want to do that. There don't seem to be any cross-field validation attributes that do this, so I guess I could build one patterned off CompareAttribute. Anyone have an example of this already built? Anyone know when MVC Contrib for MVC 3 will be released?
0

Add a custom attribute such as [RequiredIf] as explained here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.