Given the following viewmodel:
public class SomeViewModel
{
public bool IsA { get; set; }
public bool IsB { get; set; }
public bool IsC { get; set; }
//... other properties
}
I wish to create a custom attribute that validates at least one of the available properties are true. I envision being able to attach an attribute to a property and assign a group name like so:
public class SomeViewModel
{
[RequireAtLeastOneOfGroup("Group1")]
public bool IsA { get; set; }
[RequireAtLeastOneOfGroup("Group1")]
public bool IsB { get; set; }
[RequireAtLeastOneOfGroup("Group1")]
public bool IsC { get; set; }
//... other properties
[RequireAtLeastOneOfGroup("Group2")]
public bool IsY { get; set; }
[RequireAtLeastOneOfGroup("Group2")]
public bool IsZ { get; set; }
}
I would like to validate on the client-side prior to form submission as values in the form change which is why I prefer to avoid a class-level attribute if possible.
This would require both the server-side and client-side validation to locate all properties having identical group name values passed in as the parameter for the custom attribute. Is this possible? Any guidance is much appreciated.
Use of
require_from_group
from jquery-validation team:jQuery-validation project has a sub-folder in src folder called additional. You can check it here.
In that folder we have a lot of additional validation methods that are not common that is why they're not added by default.
As you see in that folder it exists so many methods that you need to choose by picking which validation method you actually need.
Based on your question, the validation method you need is named
require_from_group
from additional folder. Just download this associated file which is located here and put it into yourScripts
application folder.The documentation of this method explains this:
Why you need to choose this implementation :
This validation method is generic and works for every
input
(text, checkbox, radio etc),textarea
andselect
. This method also let you specify the minimum number of required inputs that need to be filled e.gI created two classes
RequireFromGroupAttribute
andRequireFromGroupFieldAttribute
that will help you on both server-side and client-side validationsRequireFromGroupAttribute
class definitionRequireFromGroupAttribute
only derives fromAttribute
. The class is use just for configuration e.g. setting the number of fields that need to be filled for the validation. You need to provide to this class the CSS selector class that will be used by the validation method to get all elements on the same group. Because the default number of required fields is 1 then this attribute is only used to decorate your model if the minimum requirement in the spcefied group is greater than the default number.RequireFromGroupFieldAttribute
class definitionRequireFromGroupFieldAttribute
which derives fromValidationAttribute
and implementsIClientValidatable
. You need to use this class on each property in your model that participates to your group validation. You must pass the css selector class.How to use it in your view model?
In your model here is how to use it :
By default you don't need to decorate your model with
RequireFromGroupAttribute
because the default number of required fields is 1. But if you want a number of required fields to be different to 1 you can do the following :How to use it in your view code?
Notice the group selector you specified when using
RequireFromGroupField
attribute is use in your view by specifing it as a class in each input involved in your groups.That is all for the server side validation.
Let's talk about the client side validation.
If you check the
GetClientValidationRules
implementation inRequireFromGroupFieldAttribute
class you will see I'm using the stringrequirefromgroup
and notrequire_from_group
as the name of method for theValidationType
property. That is because ASP.Net MVC only allows the name of the validation type to contain alphanumeric char and must not start with a number. So you need to add the following javascript :The javascript part is really simple because in the implementation of the adaptater function we just delegate the validation to the correct
require_from_group
method.Because it works with every type of
input
,textarea
andselect
elements, I may think this way is more generic.Hope that helps!
Here's one way to proceed (there are other ways, I am just illustrating one that would match your view model as is):
Now, let's define a controller:
and a view:
The last part that's left would be to register adapters for the client side validation:
Based on your specific requirements the code might be adapted.
I know this is an old thread but I just came across the same scenario and found a few solutions and saw one that solves Matt's question above so I thought I would share for those who come across this answer. Check out: MVC3 unobtrusive validation group of inputs
I implemented Darin's awesome answer into my application, except I added it for strings and not boolean values. This was for stuff like name/company, or phone/email. I loved it except for one minor nitpick.
I tried to submit my form without a work phone, mobile phone, home phone, or email. I got four separate validation errors client side. This is fine by me because it lets the users know exactly what field(s) can be filled in to make the error go away.
I typed in an email address. Now the single validation under email went away, but the three remained under the phone numbers. These are also no longer errors anymore.
So, I reassigned the jQuery method that checks validation to account for this. Code below. Hope it helps someone.