The required field validator in Struts can be used to check if field is non-null. It can be used in either of the following forms:

    • XML: using type=”required” attribute in <validator> or <field-validator> elements.
    • Annotation: using @RequiredFieldValidator annotation type to annotate setter method of the field or action method (plain-validator).

One example usage of this validator is to validate a field whose value is pulled from a dropdown list which may contain no items (the items are generated dynamically).

 

1. Struts Required Field Validator XML

Usage:

  • Field-validator syntax:
    <field name="fieldName">
    	<field-validator type="required">
    		<message>validation error message</message>
    	</field-validator>
    </field>

     

  • Plain-validator syntax:
    <validator type="required">
    		<param name="fieldName">myField</param>
    		<message>validation error message</message>
    </validator>

     

Parameters:

Parameter name

Description

fieldName

Name of the field to validate. Required if using plain validator syntax.

 

Struts Required Field Validator XML Examples:

  • Field-validator example:
    <field name="myJob">
    	<field-validator type="required">
    		<message>You must specify your job.</message>
    	</field-validator>
    </field>

     

  • Plain-validator example:
    <validator type="required">
    	<param name="fieldName">myJob</param>
    	<message>Please specify your current job.</message>
    </validator> 

  

2. Struts @RequiredFieldValidator Annotation

Usage: Put the @RequiredFieldValidatorannotation before the setter method or action method (in case of using plain-validator) in the following form:

@RequiredFieldValidator(param1 = "param 1 value", param2 = "param 2 value", ...)

Parameters:

Parameter name

Required

Default value

Description

message

Yes

 

validation error message.

key

No

 

i18n key for validation error message.

messageParams

No

 

Additional parameters to customize the message.

fieldName

No

 

Specifies field name in case this validator type is plain-validator.

shortCircuit

No

false

Whether this validator is short circuit.

type

No

ValidatorType.FIELD

type of the validator: field-validator (FIELD) or plain-validator (SIMPLE).

 

Struts @RequiredFieldValidator Examples:

  • Basic field-validator:
    @RequiredFieldValidator(message = "Please specify your current job.")
    public void setMyJob(String myJob) {
    	this.myJob = myJob;
    }

     

  • Specifying i18n key for the message:
    @RequiredFieldValidator(message = "Default message", key = "form.validation.job")
    public void setMyJob(String myJob) {
    	this.myJob = myJob;
    }

     

  • Plain-validator (annotating the action method):
    @RequiredFieldValidator(type = ValidatorType.SIMPLE,
    	message = "Please specify your current job.",
    	fieldName = "myJob")
    public String execute() {
    	return SUCCESS;
    } 

 

Related Struts Form Validation Tutorials:

 

Other Struts Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment