6 Replies Latest reply on Oct 15, 2013 10:58 AM by csa

    errai's databinder and gwt validation

    lewarrior22

      Hi,

      Errai's databinder and gwt validation don't goes together, I don't know if it is a design principle. When validating an object Instatiate throught the dataBinder

      {code}

      Account account = accountDataBinder.getModel();

                          Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

                          Set<ConstraintViolation<Account>> constraintViolations = validator.validate(account);

      {code}

      The validation fail. All constrainst failed, event for field I manually set values to pass the validation.  This what the debugger show me, It is not validating as a real account.       Screen Shot 2012-11-16 at 2.32.04 AM.png

       

       

      But  using the using this code :

      {code}

      Account account = new Account();

                          account.setEmail(email.getText());

                          account.setId(Integer.parseInt(id.getText()));

                          account.setLogin(login.getText());

                          account.setName(name.getText());

                          account.setPassword(password.getText());

                          account.setWebsite(webSite.getText());

               Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

               Set<ConstraintViolation<Account>> constraintViolations = validator.validate(account);

      {code}

       

      Everything is well validated for the same values setted in field. Let us get a look to the debugger if you want   Screen Shot 2012-11-16 at 2.28.02 AM.png

       

      Have you noticed the diff of the account as scoped variables ?

       

      My point was to use the databinder to retrieve my model object (it is very cool) and to be able to validate using gwt-validator or a third client validator.

       

      Thanks,

       

      Boris.

        • 1. Re: errai's databinder and gwt validation
          csa

          Hi Boris,

           

          It's not a design principle. We just haven't integrated data binding with validation yet. However, there is a workaround you should be able to use:

           

          Unwrap the data binding proxies before passing them to the validator.

           

          {code}

          Account account = ((BindableProxy<Account>) accountBinder.getModel()).unwrap();

          Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

          Set<ConstraintViolation<Account>> constraintViolations = validator.validate(account);

          {code}

           

          Cheers,

          Christian

          • 2. Re: errai's databinder and gwt validation
            lewarrior22

            It is perfect !

             

            {code}

                

            //I had to cast in account again

                                Account account = (Account) ((BindableProxy<Account>) accountDataBinder.getModel()).unwrap();

            Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
            Set<ConstraintViolation<Account>> constraintViolations = validator.validate(account);

             

                

            {code}

             

            Thanks Christians.

            • 3. Re: errai's databinder and gwt validation
              holsen

              Now that Errai 2.4 includes Errai Navigation, Errai does the work for us. However, nested objects annotated with @Valid which are used in chained data binding don't get validated properly. I think it's because their DataBinding Proxies don't get removed.

              • 4. Re: errai's databinder and gwt validation
                csa

                Hi Chris,

                 

                I am aware of that limitation when validating nested bindables with @Valid. The only clean solution I can think of is to generate our own validators (instead of using GWTs generated validators directly). However, this is quite a maintenance burden and will cause compatibility issues in the future. We were in a similar situation with our own UIBinder module.

                 

                A workaround you can use right away is to use method level annotations instead of field level annotations on your nested bindable types.

                 

                Cheers,

                Christian

                • 5. Re: errai's databinder and gwt validation
                  holsen

                  Hi Christian,

                   

                  Well my workaround is to write a dedicated helper method for each object which walks through the whole objects and unwraps all sub objects. Afterwards validation works just fine. I was hoping that you were able to write a universal method which does just that.

                   

                  Cheers,

                   

                  Christian

                  • 6. Re: errai's databinder and gwt validation
                    csa

                    Hi Chris,

                     

                    I don't see a way to do this without touching the generated validators and we might have to bite the bullet and do this in Errai 3. I'd prefer not to because of the maintenance/compatibility issues I've described above. Maybe I am missing something though. If you can think of a generic solution that doesn't involve generating our own validators, please let us know. We happily accept contributions .

                     

                    Thanks,

                    Christian