How to check select tag in struts 2 views

I am new to programming, and currently I work as a junior programmer. I am currently having some problems validating select tags in one of my forms. What I'm actually trying to do is make sure that one item is selected before the user submits the form.

In the form I am:

<s:select list="assessmentTypes" headerKey="0" headerValue=" -- Select One --" listKey="id" value="name" listValue="name" key="course.assessmenttype" name="assessmenttype.id"/> 

I have some validation information but am not sure how to do this for a select list.

I tried using a regular check with strings, but I don't think it is necessary in this case. eg,

 <field name="course.assessmenttype"> <field-validator type="requiredstring"> <message>Please Select a value</message> </field-validator> </field> 

All help would be appreciated, thanks in advance.

+4
source share
3 answers

One option is to use the int validator with the min value set. Since you want any value greater than 0.

  <field name="course.assessmenttype"> <field-validator type="int"> <param name="min">1</param> <message>Please Select a value to continue</message> </field-validator> </field> 
+1
source

Set headerKey = "0" to headerKey = ""

 <field name="course.assessmenttype"> <field-validator type="requiredstring"> <param name="trim">true</param> <message>Please Select a value to continue</message> </field-validator> </field> 
+1
source
 <field name="course.assessmenttype"> <field-validator type="regex"> <param name="expression">assessmentTypes</param> <message>Select a value to continue</message> </field-validator> </field> instead of assessmentTypes, you can directly mention the dropdown list values. Make sure that your validator XML file is in the format <ActionClassname>-validation.xml,which should be in the same package as Action class 
0
source

All Articles