2012-03-16 66 views
1

例如,我想輸入應該是nonEmptyTextemail如何在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> 

這裏,lastOptionnonEmpty

但我認爲headOption較好,但:

val loginForm = Form("email" -> (nonEmptyText verifying email)) 

不工作 - 沒有的email約束,所以我們有一個爲自己定義的?

回答

2

根據Form documentation,您可以使用verifying方法向映射添加約束條件。在Constraints object中已經定義了一些約束條件。所以你可以寫:

val loginForm = Form("email" -> (email verifying nonEmpty)) 
+0

謝謝! '驗證nonEmpty'的電子郵件應該用'()'包裝,對吧? – Freewind 2012-03-17 01:25:41

+0

請查看我更新的問題 – Freewind 2012-03-17 01:33:31

+0

第二個問題是(恕我直言)在當前播放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

相關問題