Results 1 to 4 of 4

Thread: validating form slow...

  1. #1
    Join Date
    Apr 2006
    Posts
    18

    Default validating form slow...

    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?
    1. Check if member.name already exist in DB.
    2. 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
    1. 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?
    2. Is it possible to use the id property without showing it on the form... PropertiesConstraint seems to need it on the form...


    Dirk

  2. #2

    Default

    Validation is triggered whenever a valueModel is changed, it's not directly related with a visual component. You probably want to check if those 4 times are actual changes in one of the two needed valuemodels.

    Take note that you have a Constraint which depends on two properties, thus when setting a formobject, you already have 2 calls.

    The only requirement for properties to use in constraints is that they have a valueModel, so it is possible to not show a component but still have a valuemodel (getFormModel().getValueModel() creates one if it doesn't exist).

    Besides this also take note that this validation is normally client-side to guide the user in filling in the form. If you need a way to validate a form at the back-end, it might be better to do a check before the actual commit action (eg when pressing the save button).

    Kind Regards,
    Jan

  3. #3
    Join Date
    Apr 2006
    Posts
    18

    Default

    Quote Originally Posted by jh@schaubroeck.be
    Validation is triggered whenever a valueModel is changed, it's not directly related with a visual component. You probably want to check if those 4 times are actual changes in one of the two needed valuemodels.

    Take note that you have a Constraint which depends on two properties, thus when setting a formobject, you already have 2 calls.
    Two calls seems logical, two values change. But in my output I get:
    Code:
    testing name:Dirk  id:1
    testing name:Dirk  id:1
    testing name:Dirk  id:1
    testing name:Dirk  id:1
    so values seem to be set and don't change. After the form is displayed it works fine: 1 value changes => 1 validation call

    Quote Originally Posted by jh@schaubroeck.be
    The only requirement for properties to use in constraints is that they have a valueModel, so it is possible to not show a component but still have a valuemodel (getFormModel().getValueModel() creates one if it doesn't exist).
    That is what I firstly thought, but somehow it (I) did not succeed. But it works indeed such as you describes. I got it working now.


    Quote Originally Posted by jh@schaubroeck.be
    Besides this also take note that this validation is normally client-side to guide the user in filling in the form. If you need a way to validate a form at the back-end, it might be better to do a check before the actual commit action (eg when pressing the save button).
    I'm working on standalone appllication so the validation is client-side...But you are correct, I would be better to validate data in the commit action.

    Dirk

  4. #4
    Join Date
    Apr 2006
    Posts
    18

    Default 4 times validating same values

    i reworked my query, and the validation is real quick now...

    but i cannot figure out why the form is 4 times validated (4 times the same values) before it is shown for the first time...

    anyone an idea?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •