hi,
I create a dialog to update a Member object.
Code:
class Member{
String name;
Integer id; //autogenerate by database
...some other fields
Before updating the database i want to validate the form.
Member.name must be a unique value, so how do I validate this in an update?
- Check if member.name already exist in DB.
- When a member found, check if member has same id.
Code:
public class UpdateMemberConstraint extends Rules {
Manager manager;
UpdateMemberPropertiesConstraint updater = new UpdateMemberPropertiesConstraint();
PropertiesConstraint updateConstraint = new PropertiesConstraint("name", updater, "id");
public UpdateMemberConstraint(Manager manager) {
super(Member.class);
this.manager=manager;
}
protected void initRules() {
add(updateConstraint);
}
private class UpdateMemberPropertiesConstraint implements BinaryConstraint{
public boolean test(Object name, Object id) {
System.out.println("testing");
Member m = manager.findMemberByName((String)name);
if (m==null)
return true;
return m.equals(manager.findMemberById((Integer)id));
}
public boolean test(Object arg0) {
return false;
}
}
}
I add this Rule in my DefaultRulesSource class
Code:
public class ValidationRules extends DefaultRulesSource {
public ValidationRules() {
addRules("CREATE",...);
addRules("UPDATE",new UpdateMemberConstraint(getManager()));
}
...
and configure the rulesSource-bean...
This code works, validation and update are OK.
But there is a performance problem, it takes about 3 secs before my dialog is first displayed... I know I should refactor my 2 DB-queries in a single query, and i will do that. But that is not my point.
What I found out is that my form has been validated 4 times before it is displayed. (I see 4 lines "testing" on my console)
here are my actual questions
- Why is that? It makes no sence to validate a form 4 times before showing it... is it the framework or is it my bad code?
- Is it possible to use the id property without showing it on the form... PropertiesConstraint seems to need it on the form...
Dirk