Validating two elements (e.g. Dropdown and Textbox)

Nov 5, 2009 at 11:12 PM


I want to use Xval to validate two elements e.g.

DropDown: << Not In List is selected by user >>

Textbox: <<A value must be entered into the text box >>

I can easily detect that the value for "Not in List" is selected (or not) in the drop down. However, how to now check the value in the texbox?

I've tried the following by adapting the BookingsDemo... To simulate the problem Im checking the booking name against the number of guests. If the booking name is "a" the the number of guests cannot be 0 (e.g. if Not in List is selected in the dropdown then the texbox cannot be blank). So Ive added the following code to check the condition

public static IEnumerable ValidateName(Booking booking)
if (booking.Name == "a" && booking.NumGuests == 0)
yield return new ErrorInfo("Name", "There must be people in your group");

... and in my .aspx file Ive wired up both the Name and the NumGuests to the validation as follows....

<%= Html.ClientSideValidation("booking", typeof(Booking))
            .AddRule("BookingDate", new RemoteRule(Url.Action("ValidateBookingDate")))
            .AddRule("Name", new RemoteRule(Url.Action("ValidateName")))

Here lies the problem...

1. By tabbing out of either Name I get an error message as required if the number of guests is not equal to zero. This is correct behaviour.

2. I then go into number of guests and change the value to e.g. 5.

This does then not clear the validation on Name and I cannot submit the form.

Any ideas how I can either..

a) Clear the validation on Name after I have entered the number of guests. Or...

b) Force revalidation before form submission

Thanks in advance for all responses received.

(I'm using xVal 1.0)

Jan 8, 2010 at 11:11 AM

I'd like to second this request for help.

In my example I have a credit card payment form where the Start Month/Year is entered using two dropdowns - one for the month and another for the year.

There are two validations on these fields:

  • If one is complete and the other is blank then display a message
  • If the month and year are in the future then display a message.

Really this is a single set of validations that should be applied to both fields.

The solution I envisage uses a hidden field to combine both values and be validated singluarly but this has a number of disadvantages including highlighting the user fields and adding 'working' field data to my model objects.

I fear that the only true solution to this would be for the validation rules added the client side validation to accept multiple fields with the 'primary' field being the one to have the message attached.

Jan 20, 2010 at 8:58 AM

I too need a solution to this problem.

I have a set of radio buttons that enable different options, i need to validate only the controls that are related to the selected radio.

Also, inside of some option groups there are cascading drop downs which need to be validated based on parent dropdown.

May 26, 2010 at 1:22 PM
Edited May 26, 2010 at 1:24 PM

i would also like a solution to this problem, this has been the main issue with dataannotation for me and for anyone doing real world mvc stuff (and not just those pretty (easy) the world is non-complex and simple examples we get shown by the product evangelists).

currently i am using fluent validation which is able to do this, but am not entirely happy with it, so my decision to move to xval/castle would be contingent on being able to do something like this in an elegent manner, as FluentValidator allows me to (it is great on that end).