Announcement

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

    addValidator missing from API?

    Hi,

    SmartGWT EE

    I'm creating my custom validator and in the javadoc I found the following:

    Code:
    Custom validators can be reused on the client by adding them to the global validator list, via the
     {@link com.smartgwt.client.widgets.form.validator.Validator#addValidator Validator.addValidator} method.
    I have been looking for this Validator.addValidator method everywhere, but I can't seem to find it? Could you please advice? Is this missing from the API?

    Regards,
    Bart

    #2
    Doesn't exist in SmartGWT, where it wouldn't really help reduce the amount of code the way it can in JavaScript. The shortest code achievable in Java is to just create a utility method that returns an instance of the custom validator, and use that whenever you need to apply it.

    Comment


      #3
      Euhm... so this means you will not be adding it to the API in countrary to what the javadoc states?

      Comment


        #4
        Right, the doc will be corrected.

        Comment


          #5
          Ok, that is a possible solution... but maybe not the one I was hoping for :-/

          Because, is the system intelligent enough to know that field x in my datasource has my custom validator xyz?

          I'm sorry but as I see it, the addValidator method was probably intended to add my custom validator xyz to the system so that during the datasource generation, the system knows which custom validator to bind to that field; correct?
          (Sort of like how the simpletype works: you first need to register your simpletype before you can use it).

          I am investigating all of this, because in my other post, http://forums.smartclient.com/showthread.php?t=13890, the solution suggested by Isomorphic was to create a custom validator (which I have done).

          I verified my custom validator works... but this involves setting my custom validator explicitly in my simple type by calling this.setValidators(new xyz()).
          They way we would like to be doing it is via the datasource xml in the validator section.

          Another hurdle is that you can't invoke getValidators(), so if you defined other standard validators (via datasource xml), you are in fact overwriting them with the call to setValidators, correct?
          Last edited by bade; 28 Oct 2010, 02:12.

          Comment


            #6
            Any response to this?
            Thanks

            Comment


              #7
              Hi
              We've just added an API to support this -- Validator.addValidatorDefinition()

              To use it, define a custom validator, with a condition, and then pass it into the addValidatorDefintion method (along with a name for this validator type).

              Then for any fields which define a validator of this new type (either specified in the ds.xml file, or in Java using the new 'setType(String type)' method), the custom validator will be picked up.

              This should be available in the latest nightly build.

              Let us know how you get on

              Thanks

              Comment


                #8
                Sounds great, thanks!
                I'm on holiday for a week, but I'll give it a try asap.
                Cheers,
                Bart

                Comment


                  #9
                  I tried addValidatorDefinition() with a custom validator and the condition() method gets called but attributes defined in the xml data source descriptor are not set at the time of the call. Am I missing something?
                  My custom validator looks like this:
                  Code:
                  import java.util.Date;
                  
                  import com.smartgwt.client.widgets.form.validator.CustomValidator;
                  
                  public class IsBeforeValidator
                      extends CustomValidator
                  {
                      public IsBeforeValidator()
                      {
                          setAttribute("type", "isBefore");
                      }
                  
                      @Override
                      protected boolean condition(Object value)
                      {
                          if (!(value instanceof Date)) {
                              return false;
                          }
                          // target is always null :((
                          String target = getTarget();
                          Date targetValue = getRecord().getAttributeAsDate(target);
                          if (targetValue == null) {
                              return false;
                          }
                          if (!((Date) value).before(targetValue)) {
                              return false;
                          }
                          return true;
                      }
                  
                      public void setTarget(String target)
                      {
                          setAttribute("target", target);
                      }
                  
                      public String getTarget()
                      {
                          return getAttribute("target");
                      }
                  }
                  The way I register it is :

                  Code:
                      public native static void addValidatorDefinition(String name,
                                                                       Validator validator) /*-{
                          if (validator == null) return;
                          var jsValidator = validator.@com.smartgwt.client.widgets.form.validator.Validator::getJsObj()();
                          if (jsValidator.errorMessage != null) jsValidator.defaultErrorMessage = jsValidator.errorMessage;
                          $wnd.isc.Validator.addValidatorDefinition(name, jsValidator);
                      }-*/;
                  
                  ..............
                  
                          IsBeforeValidator template = new IsBeforeValidator();
                          template.setAttribute("type", "custom");
                          addValidatorDefinition("isBefore", template);
                  And the way I attach the custom validator to fields is :
                  Code:
                  <field 
                      name="START_DATE"
                      type="date"
                      title="Start Date">
                       <validators>
                           <validator
                              type="isBefore"
                              clientOnly="true"
                              target="END_DATE"
                  	    errorMessage="Start date must be before end date!"/>
                      </validators>
                  </field>

                  Comment


                    #10
                    P.S.: I looked at the sources and it turns out that the behavior I observed is what should be expected. This does not seem right (even though it may be possible to get to the data needed through drilling down into CustomValidator.dataSourceField). One possible fix is to provide in CustomNavigator access to the validator attributes from the data source definition through a new field - e.g. "protected DataClass dataSourceValidator;". This field is to be initialized in setup() as follows:

                    @com.smartgwt.client.widgets.form.validator.CustomValidator::dataSourceValidator = @com.smartgwt.client.core.DataClass::new(Lcom/google/gwt/core/client/JavaScriptObject;)(validator);

                    and then also - cleaned up in reset(). I did all this in a class which extends CustomValidator and it works. The cost though is duplicate calls to setup() and reset() - in the base class and in the derived, so it would be good to implement this or equivalent fix in CustomValidator.

                    Comment


                      #11
                      That's an interesting suggestion - thanks.

                      We'll look be looking into this and implementing it or something similar in the near future (We'll update this report when we have the fix in place).
                      In the meantime your subclass solution should "unblock" you on this issue

                      Comment


                        #12
                        I tested it with the latest nightly build and it seems to be working,
                        except for what Nikolayo already mentioned.

                        Specific arguments are not set on the custom validator itself (eg the "target" attribute in his example).

                        Regards,
                        Bart

                        Comment


                          #13
                          I posted the issue on a separate thread (http://forums.smartclient.com/showthread.php?t=14153) and it was confirmed by Isomorphic and a fix is said to be pending.

                          In thread I mentioned above there is also a fix which I use for the time being.

                          Regards
                          Nikolay

                          Comment


                            #14
                            Any update on this proposed fix?

                            Originally posted by Isomorphic
                            We'll look be looking into this and implementing it or something similar in the near future (We'll update this report when we have the fix in place).

                            Comment


                              #15
                              Originally posted by bade
                              Any update on this proposed fix?
                              I have not checked this myself but the thread I quoted above says:

                              Originally posted by smartgwt.dev
                              The latest build has a method on CustomValidator :

                              public Map getValidatorProperties() that provides access to any user defined validator properties.
                              Regards
                              Nikolay

                              Comment

                              Working...
                              X