Help

Controls

PermLinkWikiLink

Built with Seam

You can find the full source code for this website in the Seam package in the directory /examples/wiki. It is licensed under the LGPL.

Forum: Seam Users Forum ListTopic List
06. Aug 2008, 22:20 CET | Link

I'm using a @Length(min="2",max="5") constraint on a String property. The inputText tag is required="false". Upon submit, no error is reported on the UI, but when it's persisted it throws a validation exception.

My goal is to allow this field to be empty, but if they user fills it in, it should be between 2-5 characters. Is there any way to do this with the standard annotations?

8 Replies:
07. Aug 2008, 00:17 CET | Link
Arbi Sookazian
you need to use <s:validate> or <s:validateAll> in your xhtml to trigger the hibernate validator(s) on the client-side as well. are you doing that?
07. Aug 2008, 05:29 CET | Link

Yup, my validation stuff works fine except for this case. The problem is that it validates on the ui how I want, but not at persist-time. Specifically:

1) nothing filled in, doesn't throw validation error (required="false")

2) filled in, but below or above min and max, throws validation error

3) filled in, and between min and max, doesn't throw validation error

At persist-time, 1) throws an error.

08. Aug 2008, 14:37 CET | Link

This is a mismatch between JSF and Hibernate validator, JSF sets an empty value as an empty String, not as null (which would pass the validation). Your best bet is to either write a custom length validator that allows this, or write some logic in your backing bean.

 

Read about how to report a bug.

08. Aug 2008, 15:51 CET | Link
Pete, you mean when the string is "", Seam (JSF really) does not run the validator? but still JSF place the value in the domain model?
 

Check out my book: Hibernate Search in Action

08. Aug 2008, 15:59 CET | Link

Exactly.

Also, if the field is empty in a JSF/facelet page, "" is set on the model, not null.

 

Read about how to report a bug.

08. Aug 2008, 16:57 CET | Link

Yes, this is a big bug in JSF. I have a few TODO curses in my source in places where I translate from "" to null. This should be fixed in JSF 2.0, Pete.

 

Check out my weblog or have a look at the books I wrote.

08. Aug 2008, 22:11 CET | Link

I agree with Christian. This behavior is just leaking a web form presentation limitations into the rest of the application. null is a much more sensible value.

 

Check out my book: Hibernate Search in Action

09. Aug 2008, 19:52 CET | Link

Ok, I will raise it with the JSF EG.

 

Read about how to report a bug.