Use of struts validwhen

Source: Internet
Author: User

Validwhen is mainly used for association verification. In order to verify the value of a domain, it may refer to the value of other domains for comprehensive judgment to determine whether the value of this domain meets the requirements.

1. Expressions and precautions

The input to validwhen is a Boolean expression (The anlr is used for parsing the expression). Its reference name is test, which is like:

<Var>
<Var-Name> test </var-Name>
<Var-value> Expression </var-Name>
</Var>

That is, when expression is true, the field is verified. The elements that can be used by expression include:

1) Name of other field attributes in the form, for example:
<Var>
<Var-Name> test </var-Name>
<Var-value> (color = "red") </var-Name>
</Var>
In this example, color is the attribute name of another field in the form.

2) * This * indicates the attribute name of the domain, that is, for the property itself being verified, its variable reference is * This *, for example:
<Var>
<Var-Name> test </var-Name>
<Var-value> (color = "red") and (* This *! = NULL) </var-Name>
</Var>

3) Some Common operators such as >,<=, >=, <=, and! can be used ,! =, +,-, *,/, %, And so on. For example:
<Var>
<Var-Name> test </var-Name>
<Var-value> (color = "red") and (age> = 25) </var-Name>
</Var>

4) logical operations and or can be used, for example:
<Var>
<Var-Name> test </var-Name>
<Var-value> (color = "red") and (age> = 25) or (color! = "Red") </var-Name>
</Var>
When using and or, you must note that in the same level of logical operations, only one logical operator can exist, either and or:
Xxx and yyy
Or
Xxx or yyy
If there are more than one and or combination at the same level, it is illegal. For example, the following expression is invalid:
Xxx and YYY and zzz
Or
Xxx or YYY or zzz
Or
Xxx and YYY or zzz
And so on.
To make complex expressions legal, you can change a complex expression to a multi-level expression. Each level has only one and or one or. For example:
Xxx and YYY and ZZZ are incorrect. perform a simple transformation:
(Xxx and YYY) and ZZZ or XXX and (yyy and ZZZ) are legal.

5) other valid input, for example, the literal string can be referenced using double quotation marks and single quotation marks.

2. custom error messages

Both validwhen and other verification routines can customize error messages after verification errors. Custom messages can be either global or local.

1) Global mode, that is, the message defined in A validator in the validator-rules.xml, the parameter name is MSG, for example:



<validator name="email"
            classname="org.apache.struts.validator.FieldChecks"
               method="validateEmail"
         methodParams="java.lang.Object,
                       org.apache.commons.validator.ValidatorAction,
                       org.apache.commons.validator.Field,
                       org.apache.struts.action.ActionMessages,
                       org.apache.commons.validator.Validator,
                       javax.servlet.http.HttpServletRequest"
              depends=""
                  msg="errors.email"/>

The MSG value is an attribute name in the resource bundle.

. For example, the following definition exists in the resource bundle file:
Errors. Email = {0} is an invalid e-mail address.

 

2) local mode, that is, the error message defined in field in validation. XML, in the format:
<MSG name = "validatorname" Key = "value" resource = "booleanvalue"/>
For example:


             <field property="endDate"
                    depends="validwhen,date">
                <msg name="validwhen" key="date.validwhen"/>
                <arg0 key="start time" resource="false"/>
                <arg1 key="end time" resource="false"/>
                <var>
                     <var-name>test</var-name> 
                     <var-value>((*this* != null) and (*this*>startDate))</var-value>
                </var>
             </field>

Note that
* <MSG> the value of the name attribute should be the name of a verification routine, for example, name = "validwhen", name = "date", etc, this indicates the error message displayed when the verification routine of the field (validwhen, date in this example) fails.
* If MSG information is defined globally and the key of the local MSG is the same as that of the global MSG, In the field verification, the local MSG overwrites the global MSG.
* The <arg0> to <arg4> attribute also has the name attribute, that is, the verification routine applied by the ARG parameter, such as <arg0 name = "date" Key = "label. startdate "/>, which is the error message used for date verification. Note: For a field, there are sometimes more than one verification rule, such as depends = "required, date". When an error occurs for each of the multiple verification rules, some parameter information is input, if they require the same number of parameters, names, and order, you only need to define <arg0.0..., <arg1... and so on. But what if they need different numbers, names, and sequences of parameters? If we define only one arg0 for the first input parameter, which authentication message is used for this arg0, therefore, the name attribute of the ARG label must be used to verify the service of this parameter.

3. Example

Scenario: in a form, there are two text fields used to obtain a time period. One text field inputs the start date and the other input the end date ).
Constraint: The date format is: year (four digits)-month (two digits)-day (two digits), and the end date is later than the start date.

According to the above conditions, the verification rules are as follows:


<field property="startDate"
    depends="date">
    <arg0 key="form.date.start"/>
    <var>
        <var-name>datePatternStrict</var-name>
        <var-value>yyyy-MM-dd</var-value>
    </var>
</field>
<field property="endDate"
    depends="validwhen,date">
    <msg name="validwhen" key="date.validwhen"/>
    <arg0 name="validwhen" key="form.date.start"/>
    <arg1 name="validwhen" key="form.date.end"/>
    <arg0 name="date" key="form.date.end"/>
    <var>
        <var-name>test</var-name>
        <var-value>((*this* != null) and (*this*>startDate))</var-value>
    </var>
    <var>
        <var-name>datePatternStrict</var-name>
        <var-value>yyyy-MM-dd</var-value>
    </var>
</field> 

Add the following message to the resource file:

 

Form. Date. Start = Start Date
Form. Date. End = end date
Date. validwhen = {0} cannot be greater than {1 }!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.