Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help-Method specific validation in struts 2

I am creating a sample application with two methods(execute,addEmployee) in a same action class(Employee.java). I need to validate some common fields in both the methods and I have some method specific validations. My Employee.java code is:

public class Employee extends ActionSupport{
   public String execute() 
   {
       return SUCCESS;
   }

   public String addEmployee()
   {
       return SUCCESS;
   }
}

And validation files:

Employee-validation.xml code to validate required field validations for name and age fields.

Employee-execute-validation.xml

 <field name="age">
    <field-validator type="int">
    <param name="min">10</param>
    <param name="max">40</param>
    <message>Age must be in between 10 and 40</message>
    </field-validator>
    </field> 

Employee-addEmployee-validation.xml

 <field name="gender">
    <field-validator type="int">
    <param name="min">1</param>
    <message>Please select a value</message>
    </field-validator>
    </field>

But while running, only rules defined in Employee-validation.xml works.

Please help me with this.

like image 809
Kalpana Avatar asked Jul 30 '26 07:07

Kalpana


1 Answers

Validation could be defined per action aliases not per methods.

<actionClass>-<actionAlias>-validation.xml

The actionAlias here refers to the action name in configuration not method in action class.

like image 84
Aleksandr M Avatar answered Aug 01 '26 21:08

Aleksandr M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!