This project is read-only.

Does xVal support conditional REQUIRED?

Sep 4, 2009 at 5:32 PM

Hi Steve,

Does xVal support conditional required?  By that I mean specifying that a property/element is required only if another property/element is NOT populated (and vice versa)?  Similarly, does it support a property being required based on the specific value of another property e.g. start date is only required if you select a payment type of switch card in a dropdown?  Both of these are common validation scenarios.  Actually, while I think about it, so is ORing.  e.g. The postcode element must match regex1 OR match regex2.

xVal looks very promising.  Thanks for pushing it up to CodePlex.



Sep 13, 2009 at 5:42 PM

Hi Ben

Doing an OR on multiple regexes is pretty easy. For example, to match "foo" or "bar", you can use the regex "foo|bar". However, the more general case (combining multiple rules so that A applies if and only if B is matched/not matched) is trickier.

xVal doesn't define what rules you can use - that's determined by which server-side validation framework you're using. For example, if you're using DataAnnotations, then xVal tries to support as much of that API as possible and run it on the client. Since DataAnnotations doesn't have any means to describe conditional rules (e.g., A is required if B is checked), there isn't any way for xVal to support that.

If you're using a server-side validation framework that *does* have some notion of conditional or associated rules, it should be possible to expand xVal to support that, though it's not exactly trivial. If you do go through with it and produce good-quality code with test coverage, please submit a patch so it can be included in the xVal project. Otherwise, it might be better for you just to write an ad-hoc rule in plain C# or JavaScript to implement this on the server or client.



Sep 19, 2009 at 5:55 PM

Hi Steve,

Thanks for the response.  Much appreciated.

I am looking at Fluent Validation at the moment and I don't think it lets you associate rules (although I am still at grasshopper stage with it).  However, it does let you say "only execute this rule when this expression is true".  In Fluent Validation code it would look something like this (Matches results in a Regex validator being configured):

RuleFor(c => c.CardNumber).Matches("visa-regex-here").When(c => c.CardType == "Visa");
RuleFor(c => c.CardNumber).Matches("mastercard-regex-here").When(c => c.CardType == "MasterCard");

I am going to have a look at xVal and Fluent Validation in more depth to see what would be involved in supporting this.  If anyone has any further insight feel free to jump in!



PS Thanks for reminding me about ORing in regex! ;)

Sep 20, 2009 at 11:26 AM

This thread on Fluent Validation and xVal integration may also be of interest.  It highlights some of the concepts that cannot be mapped from Fluent Validation to xVal.  However, I think they probably could be mapped from Fluent Validation to JQuery validation etc (i.e. the xVal conversion is lossy ;).  I am not yet up to speed with the xVal code so I am going to look at whether a direct mapping from FV to JQ validation works first before seeing how to patch xVal to do it.

Sep 24, 2009 at 6:31 PM



I liked the idea. But for simplicity sake, can you give me a sample code block for situation below:

if (a>0 & b==0 ) return b;


I used the following code for ComparisonRule.Operator.Equals (for two strings match):

return new RuleSet(
                    x => x.PropertyB,
                    x => (
                        (Rule)new ComparisonRule(x.PropertyA, ComparisonRule.Operator.Equals)
                            ErrorMessage = x.ErrorMessage ,
                            ErrorMessageResourceName = x.ErrorMessageResourceName,
                            ErrorMessageResourceType = x.ErrorMessageResourceType

With in RuleSet(<CustomRules>.ToLookup(....) can we perform above function?


Thanks in advance.