users@glassfish.java.net

Re: JSF validation question

From: Ryan Lubke <Ryan.Lubke_at_Sun.COM>
Date: Mon, 20 Aug 2007 13:57:41 -0700

glassfish_at_javadesktop.org wrote:
> I have a LineItem object that is part of an Order. The LineItem references a Product and contains a quantity. Because some ProductS are sold only in packs and not as individual items, I need to prevent the user from specifying an illegal quantity. For example, if the pack size = 4, the quantity can be a multiple of 4, but not 1, 2, 3, 5, etc.
>
> I'm using an <h:dataTable> with <h:inputText> for the quantity for each LineItem.
>
> I created a validator method to check the quantity the user, but I cannot figure out a way to obtain the LineItem object in the validator method. I need this so I can check the associated Product to see if the quantity entered by the user is valid.
>
> I could check the quantity in the action method when the user presses the "Update Cart" button after changing a quantity, but I'd like to avoid that because by then, the quantity has been updated and I can't get the previous quantity to display to the user. [Note: I don't write the Order to the database until it is complete, so I can't get the Order back from the db.]
>
> I tried to pass the Product's id in the UIComponent's id, by doing the following:
>
> <h:inputText id="_#{lineItem.product.id}"
>
> but it didn't work -- when i called UIInput.getId(), it only returned "_".
>
> Any ideas on how to handle this?
>
I think the best way to handle this is to create an actual Validator
implementation instead of
binding to a method.

This is a little more work as you'll have to create a Validator, JSP
tag, and a TLD entry, but it would allow
you to do something like:

  <n:quantityValidator productId="#{lineItem.product.id}" />

So when you validator is instantiated and associated with the UIInput,
it will have the product ID available
when validation is necessary.


> Thanks very much,
>
> Rick
> [Message sent by forum member 'rickhoro' (rickhoro)]
>
> http://forums.java.net/jive/thread.jspa?messageID=231547
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>