views:

344

answers:

3

i have a map and i want to validate in struts 2 validation framework by using expression validation how can i access the elements of the map dynamically?

if Map myMap; how can i validate the map with dynamic key? if mymap has static key like "Salary", i could validate like

<field
 name="myMap['Salary']">
 <field-validator
  type="regex">
  <param
  name="expression">[0-9]+[.][0-9]+</param>
  <message>${getText("errors.validation.number")}</message>
 </field-validator>
</field>

thanks,

Helen

A: 

I don't think you can do this in the declarative validation. I'd suggest dropping down to the validation method for this, or doing the validation in javascript if you're using the javascript validation that is generated by the framework.

Brian Yarger
+1  A: 

There are two ways you can use the myApp in declarative validation.

  1. By using myMap.salary
  2. By using myMap['salary']

You will need to use one of the above notations which is based on how you have your input form fields defined.

For example, if your input form looks something like below then you need to use the . operator as you are using the . operator while defining the name of input field.

<s:form action="sayHello">
<s:textfield name="myMap.salary" label="Salary">
<s:submit/>
</s:form>

If you use [] opertaor for defining the name of input field then use the [] operator to access the property in validator.

So, your validation code above is correct, you just need to define your input field at input form correctly with [] operator.

Hope this helps

Zaheer Baloch
+1  A: 

Hibernate Validator Framework has a recursive validation feature.

And there is a plugin for Struts2 that uses this:

Full Hibernate Plugin: http://cwiki.apache.org/S2PLUGINS/full-hibernate-plugin.html

Yoshi