Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Custom Validation example

    Hi Gurus

    I am new to SmartGWT and so far I am really enjoying it. I am trying to write a custom validator but couldnt get it working.

    Background of my application:
    I have a form to collect user information. This form is linked to a Data Source for persisting data. I am using Grails as back-end and using RestDataSource to write and retrieve data in my application.

    What I want:
    In this form, there is a "ID" field which should be unique. This uniqueness should be checked as soon as the form text field looses focus. For this in Grails I have created a URL which will check "success" or "failure" after checking for the "ID" in the DB.

    My approach till now:
    Coming from a JSF background, I was planning on creating a Custom Validator and attach it to the field so that it gets validated onblur.
    However when I started, I could not get the exact things I would need to get it working.

    Can someone please point me in the correct direction as to how this can be done? Any help appreciated! and thanks in advance.

    #2
    Some sample code from you would be helpful, but here is a sample on how to do this:

    Code:
    final TextItem item = new TextItem("id", "ID");
    CustomValidator validator = new CustomValidator() {
        @Override
        protected boolean condition(Object value) {
            // Put your validation check here
            return callToService(value);
        }
    };
    
    item.setValidators(validator);
    
    item.addBlurHandler(new BlurHandler() {
        @Override
        public void onBlur(BlurEvent event) {
            item.validate();
        }
    });
    Hope this was helpful.

    Comment


      #3
      Thanks for the example.
      However I am kinda stuck on how to call a REST api for the validation. For example my URL is "/person/check" which takes in a user ID and returns a 0 or 1 depending on the check.

      Is there a way to do it directly from the code? Do I need to use a datasource? Since I already have a datasource for the form, is it possible to link a field to 2 datasources?

      Thanks a lot in advance.

      Comment


        #4
        I think you can't make a call to your server (which is asynchronous) inside the "boolean condition(Object value)" method since it is synchronous.
        I don't know how to do this anynchronous validation.
        Last edited by ferranmc; 2 Jan 2012, 07:50.

        Comment


          #5
          Oh! I see. So any way of achieving it?

          Comment


            #6
            This may help.
            MichalG

            Comment


              #7
              I would do something like this:

              Code:
              // SIMPLIFIED CODE
              
              item.addBlurHandler(new BlurHandler() {
                  @Override
                  public void onBlur(BlurEvent event) {
                      service.checkValue(item.getValue(), new Callback() {
                          public void onSuccess(valueCheck check) {
                              if( ! check.isOk() ) {
                                  markItemAsWrong(item);
                              }
                          }
                      };
                  }
              });
              I don't know how to mark an item as wrong (that's what validate() does internally).

              Anyway, this solution is a bit complicated because when the user clicks submit on the form you must also call service.checkValue() and wait for the callback before you really submit the form.

              I would like to know a better solution.

              Comment


                #8
                Originally posted by michalg
                This may help.
                MichalG
                Thanks! It seems that 3.0 has added async validation.

                Comment


                  #9
                  Thanks all for your support.
                  Tried a few things and now I think I'll go with the server validation as of now. So on submit i am checking on the grails side if the user id is unique and if not then returning a json mentioned here: http://www.smartclient.com/smartgwt/showcase/#form_validation_server_json

                  I am digging into it further. Will update this ticket if I find a better solution.

                  Comment

                  Working...
                  X