RequiredIf validation with child entities
I am trying to get my MVC3 application to display the validation messages of RequiredIfs, using the attribute from here: http://blogs.msdn.com/b/simonince/archive/2011/02/04/conditional-validation-in-asp-net-mvc-3.aspx Now it works fine if there are no child entities, but I have a user, and that users address object needs to be validated. Well actually even one address would work, but there are 3 addesses, each has a checkbox that indicates whether it is active or not.
I tried changing the jQuery of the validator.unobtrustive.js as following to get the correct value on validation (address is either 'Address_' or 'BillingAddress_':
$.each(this.params, function () {
var address = element.name.split('.')[0] + '_';
paramValues[this] = this.toString() == 'dependentproperty' ? address + $element.attr(prefix + this) : $element.attr(prefix + this);
});
This only works when all possible checkboxes that a RequiredIf depends on is checked. The following validates all addesses and gives outputs the messages on the view:
$.each(this.params, function () {
paramValues[this] = this.toString() == 'dependentproperty' ? 'Address_' + $element.attr(prefix + this) : $element.attr(prefix + this);
});
Any help or ideas why it is behaving this way are welcome =) Other approches to solve the problem I'll happily accept. Thanks for your help.
Edit
I realized that the javascript stuff doesn't work anyways and am now already getting the correct ID from the model. But I still have the error that it only validates when all the checkboxes are checked. Any ideas why that is?
The Model for an Address is built as following:
[RequiredIf("IsActive", true)]
[Display(Name = UserManagementResources.sdAddressText, ResourceType = typeof(UserManagementResources))]
public string Street
{
get { return _address["sdAddress"]; }
set { _address["sdAddress"] = value; }
}
[RequiredIf("IsActive", true)]
[Display(Name = UserManag开发者_Go百科ementResources.sdCityText, ResourceType = typeof(UserManagementResources))]
public string City
{
get { return _address["sdCity"]; }
set { _address["sdCity"] = value; }
}
The ViewModel contains the different addresses.
public Address Address
{
get { return _address ?? (_address = new Address(Customer.Name, _domain, "Address") { IsActive = true }); }
}
public Address BillingAddress
{
get { return _billingAddress ?? (_billingAddress = new Address(Customer.Name, _domain, "BillingAddress")); }
}
The controller simply creates a new Empty Model. The view displays the different addresses using a Html.EditorFor().
Well, I assume your public Address properties have sets as well as gets (not shown above). One way to do this is to just disable the address editing form when the user unchecks "IsActive". Then use the jQuery validate option of ignore: ':disabled'
or if you hide them, ignore: ':hidden'
. Check out this sample that uses that:
http://jsfiddle.net/2EfPZ/21/
From this question:
Getting an error when trying to validate fields that depend on another option using jQuery Validate plug-in
精彩评论