Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toggle validation in MVC 3 Razor

I'm using MVC 3 with razor as the view engine and the unobtrusive client validation enabled.

I'm trying to create a form where the user has a radio button group to select their preferred contact method - either phone or email. Depending on the option selected, I want to show the appropriate textbox, but then also enable/disable the required validator for the appropriate textbox.

My markup looks something like this at the moment (Just starting out with MVC so please point out any obvious mistakes):

<div id="prefferedContact">
    <p>Preferred Contact Method *</p>
        <input type="radio" id="contactMethodEmail" name="PreferredContactMethod" value="email" @if (Model.PreferredContactMethod != "phone"){<text>checked="checked"</text>} /> <label for="contactMethodEmail">by email</label> 
        <input type="radio" id="contactMethodPhone" name="PreferredContactMethod" value="phone" @if (Model.PreferredContactMethod == "phone"){<text>checked="checked"</text>} /> <label for="contactMethodPhone">by phone</label>
    </div>
    <div id="contactMethodDetails" class="formItem">
        <div id="emailAddressBox">
            @Html.LabelFor(x => x.Email, "Email address")
            @Html.TextBoxFor(x => x.Email, new { @class = "textbox" })
        </div>
        <div id="phoneNumberBox">
            @Html.LabelFor(x => x.PhoneNumber, "Phone number")
            @Html.TextBoxFor(x => x.PhoneNumber, new { @class = "textbox" })
            </div>
        </div>
    </div>
</div>

There's some jquery function that adds an onclick event to the radio buttons to toggle between the two boxes depending on the selected value.

The Model - for these specific fields - doesn't have any required validation on it at the moment but is binding fine. Also, validation is working on other fields as expected

I really just need to get an idea of:

(a) is it possible to toggle validation on and off
(b) does this impact the ModelState validation in anyway (or do I need to customise it)

I had also thought of having the one textbox for the contact data, but I wanted to have regular expression validation for the email and for the phone number separately. If I was to have a single textbox, could I switch the validation rules on the textbox depending on the selected option???

Hope that's clear enough with enough information.

Thanks

Joel

like image 353
swingdoctor Avatar asked Mar 07 '26 05:03

swingdoctor


1 Answers

You can perform class-level validation if you need to enforce rules based on multiple properties:

http://weblogs.asp.net/scottgu/archive/2010/12/10/class-level-model-validation-with-ef-code-first-and-asp-net-mvc-3.aspx

Class-level validation

Unfortunately, this seems to only work server-side, so you'd have to implement custom client-side validation.

Another option would be to have two different models, one for each scenario (with common properties in a base class), but this might be a little more complicated.

like image 94
Danny Tuppeny Avatar answered Mar 08 '26 20:03

Danny Tuppeny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!