2015-04-06 58 views
1

我想用Play!Framework 2.3.8構建一個簡單的表單。因此,我創建了一個可以正常工作的單選按鈕組,但是如何預先選擇某個單選按鈕?如何在playframework中預選radiobutton-/checkbox-group?

我沒有在文檔中找到這樣的東西:對於複選框存在

Documentation radiobuttons

同樣的問題:

Documentation checkboxes

@helper.form(action = routes.Application.submit(), 'id -> "userForm"){ 
     <fieldset> 
      @helper.inputRadioGroup(
      userForm("Geschlecht"), 
      options = options("Mann"->"Mann","Frau"->"Frau"), 
      '_label -> "Gender", 
      '_error -> userForm("Geschlecht").error.map(_.withMessage("select gender")) 
      ) 
     </fieldset> 

任何想法?

回答

1

當要傳遞形式對象圖,而呈現,即形式對象必須被填充有實例具有選定字段的值Geschlecht

控制器代碼

UserForm user = new UserForm(); 
//set value of Geschlecht 
user.Geschlecht = Mann; 

Form<UserForm> form = Form.form(UserForm.class); 
form.fill(user) 

Html view = views.index.render(userForm); 

index.scala.html

@helper.form(action = routes.Application.submit(), 'id -> "userForm"){ 
     <fieldset> 
      @helper.inputRadioGroup(
      userForm("Geschlecht"), 
      options = options("Mann"->"Mann","Frau"->"Frau"), 
      '_label -> "Gender", 
      '_error -> userForm("Geschlecht").error.map(_.withMessage("select gender")) 
      ) 
     </fieldset> 

在選項地圖中,它將選擇具有相同鍵值的條目作爲字段Geschlecht 。

+0

謝謝你,必須改變我的控制器,但理解這個想法。非常有用的提示! – hamena314