• Post Reply Bookmark Topic Watch Topic
  • New Topic

Validation of Selects (Heeelp!)  RSS feed

 
Jamie Jackson
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having a problem with validation of certain form selects.

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!

Thanks,
Jamie

Form snippet:
<html:select name="grantForm" property="fiscalYear.id">
<htmlption value="" />
<htmlptions collection="fiscalYears" property="id" labelProperty="year"/>
</html:select>

Validation snippet:
<field property="fiscalYear.id" depends="required">
<arg0 key="fiscalYear.displayname" />
</field>
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jamie Jackson
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply, Merrill.

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?

Thanks,
Jamie

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.
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Jamie Jackson
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We might be getting somewhere. id is actually a Long, and setId/getId take/return a Long.

Is the id not appropriately getting set to empty string because FFY.getId() doesn't deal with (empty) strings?

Thanks,
Jamie
 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that case, using 0 as the dummy value might be a good idea.

At least now we understand WHY the required is not working. For required to work the getId would have to return null because a Long can not be an empty String.
 
Jamie Jackson
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright, so it looks like 0 ought to be the first (dummy) option.

However, since I need to display a message ("You must choose a federal fiscal year") when a user neglects to select an option, does that necessitate a whole new "validation-rule," validation class/method (Java), and client-side (JavaScript) function?

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?

Thanks again,
Jamie
[ March 10, 2005: Message edited by: Jamie Jackson ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!