You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by jimkski <ji...@hotmail.com> on 2008/01/06 22:41:10 UTC

[S2] Indexed properties, Type Conversion and Validation

Hey All-

I'm developing an application using the ModelDriven approach.  On of my
forms is a grid style data entry page and I'm having a difficult time
getting validation to work.  I used the approach specified by Patrick
Lightbody (http://struts.apache.org/2.0.11/docs/type-conversion.html) for
handling the rendering and updating of collections.  With this approach my
validations aren't firing.  I tried using the VisitorFieldValidator to push
the validations out to the Model but that isn't working.  

The code that populates the edit grid is along the following lines:

<s:iterator id="category" value="productCategories">
<tr>
<td><s:textfield name="productCategories(%{id}).sortOrder"
value="%{sortOrder}" size="2" cssClass="txt"/></td>
</tr>
</s:iterator>

The object graph underlying all this looks like this:

BrandAction->Brand->ProductCategory

The Brand is the model returned by the BrandAction's getModel() method.  

I have my validation configuration files defined in the same package as the
model classes and a validation configuration file defined for the action
with the visitor validator defined within it:

<validators>
    <field name="productCategories">
        <field-validator type="visitor">
            true
        </field-validator>
    </field>
</validators>

Has anyone successfully got declarative validation working with indexed
properties?  Can anyone suggest what I might be doing wrong here? 
-- 
View this message in context: http://www.nabble.com/-S2--Indexed-properties%2C-Type-Conversion-and-Validation-tp14654165p14654165.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [S2] Indexed properties, Type Conversion and Validation

Posted by jimkski <ji...@hotmail.com>.
I've made some progress on this on my own.  I needed to write my own version
of VisitorFieldValidator to make it work however.  

Once that was done, the model was being validated but all the error messages
were being assigned to the wrong field names.  The reason for this is that
the field names on my form are governed by the format defined in the Type
Conversion wiki entry: All the field names are defined as
collectionName(unique_id).propertyName where unique_id is the id defined by
the KeyProperties_ property entry in my model's conversion properties file.    

The VisitorFieldValidator uses the Collection index when constructing the
field name used to associate an error with a field:

 for (int i = 0; i < array.length; i++) {
            Object o = array[i];
            validateObject(fieldName + "[" + i + "]", o, visitorContext);
        }

So none of the errors get assigned back to the correct field name and then
can't be displayed properly.  

Getting the error messages associated with the right field name was a bit of
a trick however and I don't particularly like my implementation since it
seems fragile to me but I can't currently see a way around it.  

For my approach to work I needed to obtain o's unique Id and and assign it
in place of the value i in the code snippet above (i also needed to change
the square-brackets to parentheses).  I know that in all my use cases o is
going to have a getId method so I just hard-coded that.  Ideally, I'd like
to be able to use something in the xwork or OGNL APIs to determine the
KeyParameter from the conversion.properties file so that my code is more
dynamic but it seems pretty clear that all that information is tightly
encapsulated and out of reach.  Is there a sanctioned way for getting at the
KeyParameter information based on existing APIs?


-- 
View this message in context: http://www.nabble.com/-S2--Indexed-properties%2C-Type-Conversion-and-Validation-tp14654165p14676430.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org