One Form from multiple models

Hi,

I’ve been looking at this for quite a while now and the only solution I have found is https://github.com/ics-software-engineering/play-example-form.

Before I go down this route is there a way to create a form from more that one model and retain validation?
The above won’t work with validation apparently, I guess because a “proxy” object is used instead of using the models directly.

In my case I have a User model and an Address model but I want the Town and Country parts of the Address model filled at the same time as the User fills out User details and saved together.

So my question is “Is there a proper Play way of doing this in Java?” as this feels a bit hacky.

Thanks

Maybe this is what you want:

It shows how to handle parent/child forms.

Best.

Hi Marcos,

That’s perfect :smile:
I don’t need the repeating values but it shows me the construct I need to use so I’ll give it a go.

I wish there was more in the Play documentation covering situations like this in Java though, most of what I read when I hit a problem tends to be "well if you were using Scala this is how you would do it … "

I’m a one man show and for the project I’m working on I can’t spare the time to learn another language.
When I get it finished I hope to be able to take the time to learn Scala but for now Java is where I’m at and It’s one of the reasons I chose Play in the first place.
That and it scales and it isn’t LAMP based :slight_smile:

Thanks for taking the time to help, I appreciate it :smile:

Hey @Shane,

I agree with you that we can improve the docs here. Do you want to submit a pull request? Let me know so that I can help you. :slight_smile:

Best.

Hi Marcos,

That worked perfectly, thanks again.

I think it would help others if an example could be added to the documentation where two models could be handled in a single form.

Beginners struggle with the concept and personally I got a little confused on the way the models were to be combined and saved.
In the end your solution is elegant but unfortunately a bit hidden from a Play amateur :slight_smile:

It’s not an uncommon usecase I think so I would like to submit a pull request for other who come across the problem just to reduce a little frustration.

Play is brilliant in so many ways, and I love it because for the most part it makes sense.
But then sometimes it wrecks my head :smiley:

Thanks
Shane