Announcement Announcement Module
Collapse
No announcement yet.
declarative validation rules in Spring Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • declarative validation rules in Spring

    Valang validator is a Spring validator introducing declarative validation rules:

    Here are some examples:

    Code:
    <bean id="myValidator" class="org.springmodules.validation.ValangValidatorFactoryBean">
       <property name="valang"><value><!&#91;CDATA&#91;
          &#123; age &#58; age is not null &#58; 'Age is a required field.' &#125;
          &#123; age &#58; age is null or age >= 18 &#58; 'Customers must be 18 years or older.' &#125;
          &#123; valueDate &#58; valueDate is not null &#58; 'Value date is a required field.' &#125;
          &#123; valueDate &#58; valueDate is null or &#40;valueDate >= &#91;T<d&#93; and &#91;T>d&#93; > valueDate&#41; &#58;
             'Value date must be today.' &#125;
          &#123; firstName &#58; firstName has text &#58; 'First name is a required field.' &#125;
          &#123; firstName &#58; firstName has no text or length&#40;firstName&#41; <= 50 &#58; 
             'First name must be no longer than 50 characters.' &#125;
          &#123; size &#58; size has length &#58; 'Size is a required field.' &#125;
          &#123; size &#58; size has no length or upper&#40;size&#41; in 'S', 'M', 'L', 'XL' &#58;
             'Size must be either S, M, L or XL.' &#125;
          &#123; lastName &#58; lastName has text and !&#40;false&#41; = true &#58;
             'Last name is required and not false must be true.' &#125;
       &#93;&#93;></value></property>
    </bean>
    Take a look here for an overview of the Valang syntax.

    Valang validator currently lives in the Spring modules sandbox. Get the latest springmodules from CVS and run "ant sandbox.jar" to start using Valang validator.

  • #2
    Thanks for your feedback on the syntax. Two features have been added:

    1) use the question mark ("?") as a placeholder for the default property name (the key).

    2) the error key and error arguments have been added to support localization.

    Code:
       <bean id="myValidator" class="org.springmodules.validation.ValangValidatorFactoryBean">
          <property name="valang"><value><!&#91;CDATA&#91;
             &#123; age &#58; ? is not null &#58; 'Age is a required field.' &#58; 'age_required' &#125;
             &#123; age &#58; ? is null or ? >= minAge &#58; 'Customers must be &#123;0&#125; years or older.' &#58; 'not_old_enough' &#58; minAge &#125;
             &#123; valueDate &#58; ? is not null &#58; 'Value date is a required field.' &#58; 'valueDate_required' &#125;
             &#123; valueDate &#58; ? is null or &#40;? >= &#91;T<d&#93; and &#91;T>d&#93; > ?&#41; &#58;
                'Value date must be today.' &#58; 'valueDate_today' &#125;
             &#123; firstName &#58; ? has text &#58; 'First name is a required field.' &#58; 'firstName_required' &#125;
             &#123; firstName &#58; ? has no text or length&#40;firstName&#41; <= 50 &#58; 
                'First name must be no longer than &#123;0&#125; characters.' &#58; 'firstName_length' &#58; 50 &#125;
             &#123; size &#58; ? has length &#58; 'Size is a required field.' &#125;
             &#123; size &#58; ? has no length or upper&#40;?&#41; in 'S', 'M', 'L', 'XL' &#58;
                'Size must be either &#123;0&#125;, &#123;1&#125;, &#123;2&#125; or &#123;3&#125;.' &#58; 'size_error' &#58; 'S', 'M', 'L', 'XL' &#125;
             &#123; lastName &#58; ? has text and !&#40;false&#41; = true &#58;
                'Last name is required and not false must be true.' &#125;
          &#93;&#93;></value></property>
       </bean>

    Comment


    • #3
      Two questions:
      1. What does Valang give us that the Commons-Validator implementation doesn't?
      2. What's a Valang?

      Comment


      • #4
        Originally posted by rstearns01
        Two questions:
        1. What does Valang give us that the Commons-Validator implementation doesn't?
        From what I hear commons-validator is a solid validation framework. Recently there has been work on Spring integration code which makes it an option for Spring.

        Although I'm not familiar with commons-validator I would assume the integration code is especially interesting for migration projects where a painless migration of existing validation rules to Spring is required.

        If you need to write validation rules from scratch I would suggest to give Valang validator a try. It's less verbose than XML configuration and requires zero coding.
        Originally posted by rstearns01
        2. What's a Valang?
        Valang is an acronym based on "validation language".

        Comment


        • #5
          New features

          Some new features have just been added to the syntax:

          Code:
          &#123; age &#58; 
             ? >= $parameters&#40;'minAge'&#41; &#58; 
             'Customers must be &#123;0&#125; or older.' &#58;
             'not_old_enough' &#58;
             $parameters&#40;'minAge'&#41; &#125;
          This example takes a java.util.Map instance from the bean property parameters and gets the value associated with the minAge key. Notice the key can be a literal or the value of another bean property.

          Code:
          &#123; size &#58;
             ? in @sizes or ? in @$parameters&#40;'sizes'&#41; &#58;
             'Size &#123;0&#125; is incorrect.' &#58;
             'size_incorrect' &#58;
             ? &#125;
          This example takes the arguments for the IN operator from a bean property sizes or a map entry. The value must be an instance of either java.util.Collection, java.util.Iterator, java.util.Enumeration or an object array.

          People who find this looks too much like Perl notation to be a coincidence are probably correct. :wink:

          Comment


          • #6
            Originally posted by rstearns01
            1. What does Valang give us that the Commons-Validator implementation doesn't?
            It has more clear syntax: example such definitions takes more then 4 pages in commons-validator xml config.

            Comment


            • #7
              There's an article on the Spring Modules wiki on Valang validator. It discusses usage, features and syntax.

              Comment


              • #8
                Hi,
                Is there a validation rule to enforce uniqueness of single or multiple bean properties of beans in a collection?

                Thanks,
                Sanjiv

                Comment


                • #9
                  collection validation

                  If I have a collection property (actually a Set) how can the items in the collection be validated using valang? I 've looked through the minimal docs on valang but can't figure out if this is even possible.

                  Comment


                  • #10
                    More documentation about Date parser?

                    Does anybody know if there is some more documentation available about the date validation in Valang than on this article?

                    http://opensource2.atlassian.com/con...lang+validator

                    Thanks

                    Nicolas

                    Comment


                    • #11
                      Ask the question on SM mailing list - Steven Devijver the author will be happy to assist you.

                      Comment

                      Working...
                      X