Enum, POJO binding and Null

512 views
Skip to first unread message

thilo

unread,
Mar 29, 2010, 3:09:00 PM3/29/10
to play-framework
Hi all!

First of all, I like to thank all the creators and contributors of
play! The Framework is fantastic, it really brings the joy back to web
development with Java!

Here's my little problem:

I have a model class with an embedded Enum:

@Entity
public class Customer extends Model {
@Enumerated(EnumType.STRING)
public Salutation salutation;
....
}

public enum Salutation {
MISS,
MISTER,
}

In the controller I use directly the POJO for the binding:

public static void save(@Valid Customer customer) {
// validation
if(validation.hasErrors()) {
...

This works fine, if the user choose MISS or MISTER, but it is not
possible to bind a Null value. I always get validation errors:

<select id="customer.salutation" name="customer.salutation">
<option></option> <!-- problem -->
<option value="MISS">&{'miss'}</option>
<option value="MISTER">&{'mister'}</option>
</select>

Is there a way to bind these Null values or is this a bug?

Thanks in advance and kind regards

Thilo

Guillaume Bort

unread,
Mar 29, 2010, 3:55:39 PM3/29/10
to play-fr...@googlegroups.com
What is the validation error you get?

> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>
>

thilo

unread,
Mar 29, 2010, 4:20:52 PM3/29/10
to play-framework
Hi!

> What is the validation error you get?

I get a validation.invalid

I debugged play.data.binding.Binder.java, Line 82ff:

// Enums
if (Enum.class.isAssignableFrom(clazz)) {
if (value == null || value.length == 0) {
return MISSING;
}
return Enum.valueOf(clazz, value[0]);
}

with both variants


<option></option> <!-- problem -->

and
<option value=""></option> <!-- problem -->

The variable "value" (type String[]) contains one empty string
(value.length == 1), because of that, "value" is neither null nor has
length 0 and thus try to return valueOf instead of MISSING.

Regards

Thilo


On Mar 29, 9:55 pm, Guillaume Bort <guillaume.b...@gmail.com> wrote:

Nicolas Leroux

unread,
Mar 29, 2010, 4:24:53 PM3/29/10
to play-fr...@googlegroups.com
Hi,

I think I just committed a fix for this problem this afternoon. Could you grab the latest 1.0.2 rc2 and try it with it?
In your application.conf you will have to specify:

future.binding.string.value.returnNull=true

If it does not work, please report a bug and I will make sure it works.

Thanks,

Nicolas

thilo

unread,
Mar 29, 2010, 4:41:42 PM3/29/10
to play-framework
Hi!

The problem remains (1.0.2-RC2 with
future.binding.string.value.returnNull=true). I will file a bug.

IMHO you could fix it, if you test for emptiness of value[0], like:

L95ff in play.data.binding.Binder.java
// Enums
if (Enum.class.isAssignableFrom(clazz)) {
if (value == null || value.length == 0 || value[0].isEmpty()) {


return MISSING;
}
return Enum.valueOf(clazz, value[0]);
}

Thanks for your help!

Kind regards

Thilo

thilo

unread,
Mar 29, 2010, 4:51:13 PM3/29/10
to play-framework
There's already a bug report for the problem (sorry, I didn't see it):

https://bugs.launchpad.net/play/+bug/524689

Regards

Thilo

Nicolas Leroux

unread,
Mar 29, 2010, 6:08:05 PM3/29/10
to play-fr...@googlegroups.com
This is now fixed.

Thanks

Nicolas

Reply all
Reply to author
Forward
0 new messages