5 Replies Latest reply on Aug 19, 2013 11:50 AM by Subhrajyoti Moitra

    Problems with gwt validation and adding classname on DataFields

    Subhrajyoti Moitra Newbie

      Hello Errai experts, please help!!

       

      I have the below code snippet in a @Tempalted @Page composite.

      in GWT dev mode, i see the following output from the logger statements below, even if valid values are being entered.

       

      "

      .......

      00:53:02.928 [INFO] Sun Aug 18 14:44:51 IST 2013 ClientFormPage

      INFO: Firstname: asasdasdasd    

      00:53:03.070 [INFO] Sun Aug 18 14:44:52 IST 2013 ClientFormPage

      INFO: proname: firstName, propmsg: may not be null   

      ...... "

       

       

      Even when i enter valid values for the firstName field, i get a NotNull constraint validation error. This is also happening with the ItemFormPage.

       

      Please help. i am stuck....and not able to resolve the issue. Some pointer as how to debug this issue would be very helpfull.

       

      @Page

      @Templated

      public class ClientFormPage extends Composite{

       

      @Inject @AutoBound

          DataBinder<Client> clientDataBinder;

          @DataField

          Element firstNameControlGroup= DOM.createDiv();

          @DataField

          Element firstNameInlineHelp=DOM.createDiv();

          @Inject @DataField

          Button saveClientButton;

          @Inject @Bound @DataField

          TextBox firstName;

          @Inject

          Scheduler scheduler;

       

      @EventHandler("saveClientButton")

      public void handleSaveClientButtonClick(ClickEvent event){

              Client dirtyClient=clientDataBinder.getModel();

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

              logger.info("Firstname: "+dirtyClient.getFirstName());

              Set<ConstraintViolation<Client>> constraintViolations = validator.validate(dirtyClient);

              if(constraintViolations!=null){

                  for(ConstraintViolation<Client> clientConstraintViolation:constraintViolations){

                      final String propName=clientConstraintViolation.getPropertyPath().toString();

                      final String propMsg=clientConstraintViolation.getMessage();

                      logger.info("proname: "+propName+", propmsg: "+propMsg);

                      Scheduler.ScheduledCommand cmd=new Scheduler.ScheduledCommand() {

                          @Override

                          public void execute() {

                              setIndividualErrors(propName, propMsg);

                          }

                      };

                      scheduler.scheduleFinally(cmd);

                  }

              }

          }

       

          private void setIndividualErrors(String propName, String propMsg) {

              if(propName!=null && "firstName".equals(propName)){

                  firstNameControlGroup.removeClassName("success");

                  firstNameControlGroup.addClassName("error");

                  firstNameInlineHelp.setInnerText(propMsg);

              }else{

                  firstNameInlineHelp.setInnerText("");

                  firstNameControlGroup.removeClassName("error");

                  firstNameControlGroup.addClassName("success");

              }

       

       

          }

      }