2015-12-21 77 views
0

我想向我們的表單添加一些驗證字段。這是第一個。下面的驗證。此刻,即使它沒有通過新的驗證,它也會檢查驗證的其餘部分,並返回幾條錯誤消息。 如果該字段未通過我的新驗證,我希望它不會執行所有其他驗證並僅返回。所以基本上,如果我的字段匹配臨時字段的正則表達式,我希望它返回一個錯誤消息,而不繼續其餘的驗證。如何爲不同錯誤的字段進行scala播放表單驗證

val form = Form(
mapping(
    "my_field" -> text 
    .verifying(Messages("error.field.temporary"), x => x.matches(TEMP_FIELD)) 
    .verifying(Messages("error.mandatory", Messages("field")), _.length > 0) 
    .verifying(Messages("error.field.invalid"), x => x.length == 0 || MyField.isValid(x.toUpperCase())) 
    .verifying(Messages("error.field.invalid"), x => !MyField.isValid(x.toUpperCase()) || x.takeRight(1).toUpperCase().matches(FIELD_SUFFIX_REGEX)))) 
(MemberDetails.apply)(MemberDetails.unapply)) 

任何幫助表示讚賞。

+1

執行附加驗證的缺點是什麼? 「MyField.isValid」方法會產生很大的成本嗎? – josephpconley

+0

不,但是當您只應得到1個錯誤消息時,您會收到1個以上的錯誤消息。 – user1584120

回答

相關問題