Gunnar wrote:
Code:
ConstraintMapping mapping = new ConstraintMapping();
mapping
.type(Order.class)
.property("customer", ElementType.FIELD)
.constraint(NotNullDef.class)
.groups(MyGroup.class)
.property("orderLines", ElementType.FIELD)
.constraint(SizeDef.class)
.min(1)
.message("An order must contain at least one order line")
.type(Customer.class)
.property("name", ElementType.FIELD)
.constraint(NotNullDef.class);
If you decide to give the programmatic API a try I'd be very interested in your feedback :-)
Some thoughts about that:
In our system the user can create (on runtime) different products and also different constraints. As an example, first lots of products are added and afterwards the user add a MinConstraint to the attribute
code for type
product . After saving the min constraint the following code is (will be) executed
Code:
ConstraintMapping mapping = getConstraintMappingFromSomewhere();
mapping.type(currentConstraint.getType())
.property(currentConstraint.getQualifier(), ElementType.FIELD)
.constraint(currentConstraint.getAnnotationClass());
So far, so nice. This fragment also works wenn adding another constraint to the same class.
But what when the user want to modify or remove an existing one?
I think first I have to get all defined constraints with
Code:
Class clazz = getClassWhereTheConstraintsAreDefined();
List constraintsOfTheGivenType = mapping.getConstraintConfig().get(clazz);
and then find and modify the constraint.
What I miss (or didn't find yet) in an easy access to defined constraints. Nevertheless the validation framework is (always) reloaded after modifying any constraints.
On a additional thought I'll convert somehow our constraint types into a ConstraintDef object. :)