validating a list of objects

551 views
Skip to first unread message

Alison Winters

unread,
Nov 16, 2010, 3:56:25 PM11/16/10
to play-framework
Hi guys,

I'm struggling with validating a list of bound objects. For example, I
have the following

public class Thing extends Model {
@Required
public String blob;
}

public class ThingFormPost {
@Required
public int numThings;

public List<Thing> things;
}

I have tried adding @Required and @Valid before the "things"
declaration and neither of these cause the individual Thing objects in
the list to be automatically validated. Is there any way to do this,
or do I have to manually iterate through the list and validate each
object like:

public class Things extends Controller {

public static postThings(ThingFormPost things) {
for (int i = 0; i < things.numThings; i++) {
validation.valid("things.things[" + i + "]",
things.things.get(i));
}
}
}

This way is kinda ugly, it'd be nice to avoid it if possible.

Alison

Guillaume Bort

unread,
Nov 18, 2010, 4:46:16 AM11/18/10
to play-fr...@googlegroups.com
Please add a feature request for that.

> --
> 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.
>
>

--
Guillaume Bort, http://guillaume.bort.fr

For anything work-related, use g...@zenexity.fr; for everything else,
write guillau...@gmail.com

Alison Winters

unread,
Nov 18, 2010, 5:01:20 PM11/18/10
to play-framework
Thanks Guillaume, Lighthouse opened at:
http://play.lighthouseapp.com/projects/57987-play-framework/tickets/426-allow-automatic-validation-of-a-list-of-bound-objects

Alison
> Guillaume Bort,http://guillaume.bort.fr
>
> For anything work-related, use g...@zenexity.fr; for everything else,
> write guillaume.b...@gmail.com

Mike | Vannik Software

unread,
Mar 24, 2012, 4:27:16 PM3/24/12
to play-fr...@googlegroups.com
Hi,

It's nice that collections are supported. We have a case that looks like this:

public class MetaData {
    @Required(message = "validation.required.metaData.type")
    public String type;

    public String sourceUrl;
}

public class Item {
    public MetaData metaData;
}


Unfortunately, "type" in MetaData is not validated with the following method signature in a controller:

public static void save(@Valid Item item) {
}


Is there anything missing or is this kind of nested object validation not supported? We use Play 1.2.4.

> > To post to this group, send email to play-framework@googlegroups.​com.
> > To unsubscribe from this group, send email to play-framework+unsubscribe@​googlegroups.com.

Mike | Vannik Software

unread,
Apr 4, 2012, 5:44:31 PM4/4/12
to play-fr...@googlegroups.com
Does anyone have an idea on how to solve that?

Thanks

Peter Hilton - committer (Lunatech)

unread,
Apr 4, 2012, 6:12:28 PM4/4/12
to play-fr...@googlegroups.com
As far as I know, you can use @Valid more than once for the nesting, so add it to the nested property in Item. In other words, this might work:

public class Item {
    @Valid
    public MetaData metaData;
}

Mike | Vannik Software

unread,
Apr 5, 2012, 3:14:16 AM4/5/12
to play-fr...@googlegroups.com
Thanks Peter,

I've just looked into that again and it looks like there's also a @Required annotation necessary on the metaData property. In other words, if metaData is null, the @Required check throws an error.
If metaData is not null, but metaData.type is null, both the @Required check of the metaData.type and the @Valid annotation on the item.metaData property throw errors.

Cheers,

Mike
Reply all
Reply to author
Forward
0 new messages