Hi all,
I am having a problem with a validation scenario I am coding up. I have a (simplified) POJO as follows:
What I want to do is create a Constraint that requires the businessName property to be valid, but only if the isCustomer property is true (otherwise don't test the businessName property).Code:public class UserDefaults{ private boolean isCustomer; private String businessName; .. etc, etc. }
I tried to do this in my RulesSource implementation:
The problem is - this doesn't work, because the isTrue constraint is being evaluated against the value of the businessName property (which is a String, of course) and as such throws an exception because the String value has no such property "isCustomer".Code:private Rules createUserRules() { return new Rules(BatchConsSettings.class){ protected void initRules() { Constraint isCustomer = eq("isCustomer", Boolean.TRUE); this.add("businessName", ifTrue(isCustomer, required("businessName"))); } }; }
Unfortunately, I can't do this...
...because the add method only permits CompundConstraints and PropertyConstraints, not basic Constraints.Code:this.add(ifTrue(isCustomer, required("businessName")));
So I'm kind of stuck with this. Is such a chained constraint even possible using the existing Constraint classes, or does it require some sort of custom implementation?
-Scott


Reply With Quote
