Very common scenario, I would think: My form deals with a bean ("Grant") with several contained beans (such as "FederalFiscalYear").
In my form, I'm allowing the user to choose the FederalFiscalYear via a select box. The value being passed is the FederalFiscalYear's ID, so the select's name is "fiscalYear.id"
Almost everything works great:
* Insertion of the Grant (including the generation of the FederalFiscalYear bean upon submit)
* Correct re-display of the selected element on validation error
* Client-side validation
Here's the part that doesn't work:
* Server-side validation doesn't fire (for these contained beans only, the rest of the fields work, and so do selects that aren't representations of contained objects)
Any ideas? I've been stuck for days!
<html:select name="grantForm" property="fiscalYear.id">
<htmlption value="" />
<htmlptions collection="fiscalYears" property="id" labelProperty="year"/>
<field property="fiscalYear.id" depends="required">
<arg0 key="fiscalYear.displayname" />
A couple things:
First, there is a requirement to make the user explicitly choose a selection, so we don't want to select the first item by default. So, you're right, there is a dummy first option, however the value is currently the empty string.
Second, if I drop in a superfluous select in the form, and validate that as required, the validation works as expected. It seems that when the field is tied to the ValidationForm, it gets ignored (but only server-side, because client-side works).
Can you explain that disconnect to me?
P.S. That'll be a decent workaround, if I change the default selection to a value of 0 and validate on minimum value, so thanks for that idea.
Originally posted by Merrill Higginson:
For a select control, a "required" dependency doesn't make a lot of sense, since by nature, a select box will select the first item in the list. A lot of times, the first item in the list is something like "select option" with a zero or negative value. However, the struts validation doesn't know this isn't a valid option. You may have to get more specific than just "required" in order to get Struts to do the validation you want.
"required" does make sense when the first item (the dummy value) is empty string.
getFiscalYear().setId("") is probably not setting the id to empty string. Maybe id's setter method has logic to prevent this?
That seems like a lot of work for such a common validation. Is this validation less common than I think? Otherwise, is there some other, better approach to this?
I'm a rookie, but I'm trying to tackle all my problems with the "strutsiest" solutions possible. As I discover and apply new struts features, I see my code get cleaner and cleaner. I'm pretty excited that my JSP is comprised of clean, clever custom tags, and completely devoid of scriptlets (the beasts that gave me such a bad initial impression of JSP).
Please help me solve this latest problem in the "strutsiest" way possible?
[ March 10, 2005: Message edited by: Jamie Jackson ]