I'm having some success building a multiline/collection form as shown below,
but I'm wondering  if I can get anything from the struts2 validation
framework. Here's a (very) cut down version of my form. (I'm using a
heaviliy modified xhtml theme which requires me to use the tr tags shown
below.) The code displays a form/table, where each line in the table
corresponds to an element/item in the stocksMovements collection of the
action.

<s:iterator value="stocksMovements" status="status">

  <s:set name="mvtQtyName"
value="%{'stocksMovements['+#status.index+'].mvtQty'}" scope="request"/>
  <s:set name="commentsName"
value="%{'stocksMovements['+#status.index+'].comments'}" scope="request"/>
  <s:set name="expiryDtName"
value="%{'stocksMovements['+#status.index+'].compId.expiryDt'}"
scope="request"/>

  <tr>
    <s:textfield  name="${mvtQtyName}" value="%{mvtQty}" />
    <s:textfield name="${expiryDtName}" value="%{compId.expiryDt'}">
    <s:textarea name="${commentsName}" value="%{comments}" cols="30"
rows="2"/>
  </tr>

</s:iterator>


This generates, for example, for one column of the table 
  <input type="text" name="stocksMovements[0].compId.expiryDt" value=""
id="..."/>
...
  <input type="text" name="stocksMovements[1].compId.expiryDt" value=""
id="..."/>
...
  <input type="text" name="stocksMovements[2].compId.expiryDt" value=""
id="..."/>


For reference, the action bean is
        private List<StocksMovement> stocksMovements;

where StocksMovement is

public class StocksMovement extends ... {
    private StocksMovementPK compId;
    private String mvtComments;
    private Integer mvtQty;

and the field compId is a composite primary key (from the DB) 

public class StocksMovementPK  ... {
    private Integer locationId;
    private Integer partId;
    private Date expiryDt;

Now, on form submit with correct data I get the collection populated
correctly which is IMPRESSIVE. This is all the more amazing if you look at
the expiryDt field. Its not just a field in an element of the collection,
but a field in a field in an element of the collection. Amazing!

My question is whether I can get the struts2 validation framework involved
in validating the data, in particular the date field. Of course I can always
do manual validation in the action, but I think that this means that I'll
have to add a String "expiryDtAsString field to my nice pristine
StocksMovement model class.

"Standard" non-visitor validation requires me to know the fixed names of my
fields, and of course here there can be any number. I suppose I could
duplicate the definition 20 times or so:

    <field name="stocksMovements[0].compId.expiryDt">
        <field-validator type="date">
            <message key="errors.date"/>
        </field-validator>
    </field>    
    <field name="stocksMovements[1].compId.expiryDt">
        <field-validator type="date">
            <message key="errors.date"/>
        </field-validator>
    </field>
    <field name="stocksMovements[2].compId.expiryDt">
        <field-validator type="date">
            <message key="errors.date"/>
        </field-validator>
    </field>
        
The above does work, and sucessfully generates generates client side
javascript that works apart from some issues with error message display, but
thats my problem.

But duplicating the definition is not nice. Shame really because it seems to
work. Can wildcards be used? 
    <field name="stocksMovements\[.*\].compId.expiryDt">
        <field-validator type="date">
            <message key="errors.date"/>
        </field-validator>
    </field>

(I thought about using trying visitor validation but I am not keen because
(a) client side validation code doesn't get generated when I use visitor
validation (not sure if its me or struts 2.0.1) and (b) the syntax of that
approach in my case is far from obvious).

Is there a way of using the struts2 field validation framework with fields
like this?





-- 
View this message in context: 
http://www.nabble.com/-s2--mutiline-collection-field-validation-tf3020670.html#a8389345
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

  • [s2] mutiline/collection field validation Tim Azzopardi

Reply via email to