2013-07-12 36 views
0

在我看來,我創建了一個單選按鈕,值爲0,1,2(字符串)。Play(Scala)自定義表單類型驗證

inputRadioGroup(
    dayForm("time"), 
    options("0"->"Morning","1"->"Afternoon","2"->"Night")) 

在我的模型中,我使用時間作爲整數值,而radiobox沒有提供輸入爲整數的選項。

因此,在我的控制器中,我將時間輸入轉換爲整數,並檢查值是否在0-2之間。

val dayForm = Form(
    mapping(
    "id" -> optional(longNumber), 
    "time" -> <what can I do here to avoid type mismatch?>, 
    "date" -> sqlDate("yyyy-MM-dd") (Entry.apply)(Entry.unapply) 

有沒有辦法將時間轉換爲int,然後驗證它是否爲0-2之間的數字?像這樣:

"time" -> number (min =0, max =2) 

這將導致表單無法驗證,因爲輸入字符串尚未轉換。

回答

0

我想你可以寫

val dayForm = Form(
    mapping(
    "id" -> optional(longNumber), 
    "time" -> number (min =0, max =2), 
    "date" -> sqlDate("yyyy-MM-dd") 
    )((id, time, date) => Entry(id, convertFunction(time), date)) 
    ((entry: Entry) => Some(entry.id, inverseFunction(entry.time), entry.date)) 
)