Jordan,

I've attached a simple document I wrote for internal use by our developers on how to use the Validator tags. This might help. Let me comment inline too....

Jordan Thomas wrote:
    /**
     * Sets the username
     *
     * @struts.validator
     *     type="required,minlength,maxlength"
You should create a separate @struts.validator type="..." for each of these three validations.

     *     arg0resource="label.login.username"
     *     arg1value="minlength"
     *     arg1resource="${var:minlength}"
     *     arg2value="maxlength"
     *     arg2resource="${var:maxlength}"
And separate the arguments for each of the validators as well, so that the ones that are common to all validators are specified using @struts.validator-args and ones that are specific to a validator are specified on the @struts.validator line. See the attached document for more details on this.


     * @struts.validator-var
     *     name="minlength"
     *     value="2"
     * @struts.validator-var
     *     name="maxlength"
     *     value="16"
The vars look fine.

                  <arg0 key="loginForm.username"/>
This is generated if no other arg0 (common to all validators) is specified. Its following the naming conventions I use, but may not be applicable to your environment.

                  <arg0
                      name="required,minlength,maxlength"
                    key="label.login.username"
                  />
Notice the comma separated list here... this is not correct and stems from the way you used @struts.validator with a comma-separated list. While thats how validation.xml works, its not how @struts.validator works - its designed to be cleaner than validation.xml :)

I don't want the first line "<arg0 key="loginForm.username"
resrouce="true"/>"
If you don't want that generated then supply your own with @struts.validator-args. It won't interfere with validator specific arguments, those take precedence.

<arg1 name="minlength" key="${var:minlength}" resource="false"/>

Is this functionality working or am I just doing it incorrectly?
You're missing something here. You cannot specify arg1value and arg1resource at the same time. They are mutually exclusive. arguments do not have names, only values or keys. You're after the validator-var pieces. See the attached document to see if that helps.

Let me know if you still have problems/questions.

Erik
Title: Struts Validator XDoclet Documentation

Struts Validator XDoclet Documentation

Rather than hand-maintain Struts Validator validation.xml configuration, an XDoclet generation process has been generated to do this automatically based on metdata from an org.apache.struts.validator.ValidatorForm subclass.

For example, the following ValidatorForm subclass (BaseForm is a custom subclass of ValidatorForm), the @tags on field setters are used by the XDoclet subtask to generate the desired validation constraints for these fields.

package edu.darden.alumni;

import edu.darden.common.struts.BaseForm;

/**
 * @struts.form name="SomeForm"
 */
public class SomeForm extends BaseForm {
    private String someField;

    public String getSomeField() {
        return someField;
    }

    /**
     * @struts.validator type="required" msgkey="some.key"
     */
    public void setSomeField(String someField) {
        this.someField = someField;
    }
}

The Validator configuration is flexible and a bit tricky to understand at first glance, and its well worth several passes through the Validator documentation. There are three @tags used by the Validator XDoclet template:

  • @struts.validator
  • @struts.validator-args
  • @struts.validator-var

These tags are only valid on the setter methods.

Validator types

For each validator type desired, add an @struts.validator tag in this manner:

    @struts.validator type="required"

For a required field, this is all that is needed (except setting the field name in the ApplicationResources.properties file as shown later).

Validator arguments

Error messages to Validator are stored in ApplicationResources.properties. Arguments to the error messages generated by Validator are specified either for all validator types for the field, or specific to a validator type. Arguments common to all validator types for this field are specified using the @struts.validator-args tag in this manner:

    @struts.validator-args arg0value="A value" arg1resource="aresource.key"

And type-specific arguments are specified on the corresponding @struts.validator tag:

    @struts.validator type="range" arg1value="10" arg2value="20"

If no argument index 0 is specified using @struts.validator-args (either resource or value) then one is automatically generated using the form name and field name as a resource key.  In the example above, the validation.xml for SomeForm would be generated as:

  <form name="SomeForm">
    <field property="someField"
           depends="required">
      <msg name="required" key="some.key"/>
      <arg0 key="SomeForm.someField"/>
    </field>
  </form>

Validator variables

Some validators require variables to be defined, such as the mask validator. These are defined using the @struts.validator-var. A mask validator is defined in this manner:

    @struts.validator type="mask"
    @struts.validator-var name="mask" value="^\d{5}(-\d{4})?$"

Tag Documentation

@struts.validator

Defines a single validator type to be associated with this field.

Parameter name Description Required?
type Validator type, such as "required" Yes
msgkey Override key for the validator error message No
argNvalue Literal value for argument N of the error message. N must be between 0 to 3, respectively. No
argNresource Resource key to lookup for argument N of the error message. N must be between 0 to 3, respectively. No

Note: Only one of each argument number should be used.  For example, it would be invalid to specify both an arg1value and an arg1resource parameter.

@struts.validator-args

Defines arguments to use for all validator types.

Parameter name Description Required?
argNvalue Literal value for argument N of the error message. N must be between 0 to 3, respectively. No
argNresource Resource key to lookup for argument N of the error message. N must be between 0 to 3, respectively. No

Note: Only one of each argument number should be used.  For example, it would be invalid to specify both an arg1value and an arg1resource parameter.

@struts.validator-var

Parameter name Description Required?
name Variable name. Yes
value Variable value. Yes

 

Reply via email to