例如,我想輸入應該是nonEmptyText
和email
。如何在play2中將2個驗證器組合在一起?
如果用戶沒有輸入任何內容,則會顯示錯誤信息The field is required
,如果用戶輸入無效的電子郵件,則會顯示Invalid email
。
但是我發現,我可以只使用一個:
val loginForm = Form("email"->nonEmptyText)
或:
val loginForm = Form("email"->email)
對於後一個,如果用戶沒有輸入,它仍然會顯示Invalid email
,這是不是我想要的。
UPDATE1
我試圖Julien's answer:
val loginForm = Form("email" -> (email verifying nonEmpty))
當用戶輸入什麼,該錯誤信息是:
Valid email required, This field is required
貌似遊戲結合了email
兩個錯誤消息和nonEmpty
在一起,但這不完全是我想要的。我希望,當用戶不輸入,消息僅是:
This field is required
不:
Valid email required, This field is required
UPDATE2
我發現它爲什麼顯示綜合誤差是在helper.twitterBootstrapFieldConstructor.scala.html
,那裏的原因是:
<span class="help-inline">@elements.errors(elements.lang).mkString(",")</span>
它結合了所有的與,
錯誤。
對於我來說,這應該是:
<span class="help-inline">@elements.errors(elements.lang).lastOption</span>
這裏,lastOption
是nonEmpty
。
但我認爲headOption
較好,但:
val loginForm = Form("email" -> (nonEmptyText verifying email))
不工作 - 沒有的email
約束,所以我們有一個爲自己定義的?
謝謝! '驗證nonEmpty'的電子郵件應該用'()'包裝,對吧? – Freewind 2012-03-17 01:25:41
請查看我更新的問題 – Freewind 2012-03-17 01:33:31
第二個問題是(恕我直言)在當前播放2中的錯誤。0,請參閱[此問題](https://play.lighthouseapp.com/projects/82401/tickets/249-the-email-mapping-should-not-raise-a-validation-error-when-the-value-是空#票-249-1)。 你對'()'是對的 – 2012-03-17 08:56:23