Play Framework 2 Link the request form

I am new to Play2 (I have already developed a project using Play1), and I have problems linking the form to the request. The form documentation is really bright.

Here is the code of my controller:

private final static Form<Estimation> estimationForm = form(Estimation.class); /** * Get an estimation by form * @return */ public static Result estimation() { return ok(views.html.rate.estimation.render( estimationForm, City.findAll() )); } /** * Display estimation results * @return */ public static Result results() { if (request().method().equals("POST")) { Form<Estimation> form = estimationForm.bindFromRequest(); if (form.hasErrors()) { System.out.println(form.errorsAsJson().toString()); return ok(views.html.rate.estimation.render( form City.findAll() )); } else { System.out.println(form.get()); return ok(views.html.rate.results.render( )); } } else { return estimation(); } } 

I show the cities in the list selected as follows:

 <select id="city" name="city"> <option value="1">Paris, France</option> <option value="2">Lyon, France</option> <option value="3">Marseille, France</option> <option value="4">Barcelona, Spain</option> <option value="5">Berlin, Germany</option> </select> 

When I submit the form, I have the following error: {"city": ["Invalid value"]}

So here is my question: Binding seems to work well with simple fields (like the String property in my model, for example), but what about @ManyToOne bindings?

Thanks.

+6
source share
1 answer

Set the name of the selection field as name="city.id"

+9
source

Source: https://habr.com/ru/post/923805/


All Articles