retrieving data from a html select form with Play Framework
as the title say, i'm trying to retrieve the selected item from a html select form. I tried to do so with the following code but it doesn't work ! the city value's always NULL but the address field return correct value ...
The form.html:
#{extends '/Admin/admin.html' /}
#{form @save(id)}
#{ifErrors}
<p class="error">
Please correct these errors.
</p>
#{/ifErrors}
<p>
<label>Adresse</label>
<input type="text" name="adress" value="${flash.adress}" id="adress" />
<span class="error">#{error 'adress' /}
</p>
<p>
<label>Ville</label>
<select size="1" >
#{list items:cities, as:'city'}
<option name="city" id="city" value="${f开发者_运维技巧lash.city}">${city}</option>
#{/list}
</select>
<span class="error">#{error 'city' /}
</p>
<p>
<input type="submit" value="Publier l'annonce" />
</p>
#{/form}
the save method:
public static void save(long id, String adress, @Required Place city){
System.out.println(city);
Admin.index();
}
You must name your select tag not option tag and you must use the correct field :
<select size="1" name="city.id">
#{list items:cities, as:'city'}
<option value="${city.id}">${city.name}</option>
#{/list}
</select>
See the doc for more information on binding : Doc
It won't work like that, it should be like:
public static void save(long id, String adress, @Required String city){
// in order or so I think...: get the city ID or whatever, process all the data and save
System.out.println(city);
Admin.index();
}
There's another way which I believe it's the one you are looking for:
http://www.playframework.org/documentation/1.1.1/controllers#POJOobjectbinding
#{select 'city', value:C2, id:'select1'}
#{option 'C1'}City1#{/option}#{option 'C2'}City2#{/option}#{option 'C3'}City3#{/option}
#{/select}
精彩评论