Announcement

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

    Best way to declare client and server side validators

    My aim is to declare validators only once to be used on both sides and reuse the validator code. Some of them have to be coded in Java.


    I am searching for the best way how to define validators in the .ds.xml file, which should be used on client and on server.
    I've also checked http://forums.smartclient.com/showth...ght=validation , but still no idea how to actually do it.

    Is there a best approach like how the buildin validators from SmartGWT are used?

    Would be very nice if you can provide some code, please.

    As far as I know:

    Server validation can be achieved like this:
    Code:
    <field name="quantity" type="integer">  
                <validators>  
                    <validator type="serverCustom">  
                        <serverObject lookupStyle="new" className="com.smartgwt.sample.showcase.server.ValidatorDMI"/>  
                        <errorMessage>Only $available in stock</errorMessage>  
                    </validator>  
                </validators>  
            </field>
    Client side validation can be achieved like this:

    onModuleLoad(){
    Validator.addValidatorDefinition("myCustomValidatorType", new Validator());

    in ds.xml
    Code:
    <field name="quantity" type="integer">  
                <validators>  
                    <validator type="myCustomValidatorType">  
                        <errorMessage>validation error</errorMessage>  
                    </validator>  
                </validators>  
            </field>
    Last edited by damend; 26 May 2011, 06:38.

    #2
    That's the right way to do it if you need to add both client and server-side validation behavior to a field.

    Note that it's very very rare that there's a validation rule that isn't covered by the built-in validators and where it's worthwhile to build a client-side version of it.

    Comment


      #3
      Trying one of the examples in the showcase,
      and
      Using 2.5.2011-05-19-EVAL

      I want to test custom client and server validators with an easy example.

      My question: form.setDisableValidation(true);
      Should only run server side validation, right?
      For me it seems like, even server-side validation will be ignored.
      You have an idea, why this happens?

      Here is my code:

      Code:
      <field name="pan" type="text" length="4" operator="equals" >
          	<validators>  
          			<validator type="serverCustom" > 
                          <serverObject 
                          lookupStyle="new"                       
      className="gwt.wep.server.validator.DigitValidator" 
                          />  
                          <errorMessage>no digit</errorMessage>  
                      </validator>  
                  </validators>  
          </field>
      
      ...
      
         <operationBinding operationType="add" requires="false"/>
          <operationBinding operationType="update" requires="false"/>
          <operationBinding operationType="remove" requires="false"/>
          <operationBinding operationType="validate" requires="true"/>
          <operationBinding operationType="custom" requires="true"/> 
        </operationBindings>
      </DataSource>
      And

      package gwt.wep.server.validator;
      import java.util.Map;
      import com.isomorphic.datasource.Validator;

      public class DigitValidator {
      protected boolean condition(Object value, Validator validator, String fieldName, Map record) {
      return false;
      }
      }

      On client-code I call
      form.setDisableValidation(true);
      form.validate(true)

      Comment


        #4
        setDisableValidation disables validation altogether, including automatic trips to the server to run server-side validators. Validation still runs server-side if an actual save is attempted of course.

        Comment


          #5
          If setDisableValidation disables validation alltogether, how to disable client validation?
          Having a look on the example and the API tells a different story:

          http://www.smartclient.com/smartgwtee/showcase/#validation_form

          Comment


            #6
            Client validation includes *client-initiated* on the fly calls to the server to do validation of specific fields.

            This setting exists principally for debugging and would not be used in real development. What problem are you trying to solve?

            Comment


              #7
              My overall goal is to have client-side validation and server validation for the service.

              So, if I have to explicity define client and serverside validators separately in the ds.xml file , is it also possible for SmartGWT and best practice to put these validator classes in a .shared package, so that they can be used for client and server validation ?
              Thanks for reply -

              Comment


                #8
                If there's non-trivial common logic (this is rare), then yes, factor it out and put it in a shared class.

                Comment

                Working...
                X