2012-10-15 13 views
7

स्कैला फ़ॉर्म सहायक में "nonEmptyText" कॉन्स्टेंट का उपयोग करते समय मैं डिफ़ॉल्ट त्रुटि संदेश "यह फ़ील्ड आवश्यक है" को कस्टमाइज़ करना चाहता हूं।Play Framework/Scala रूपों में फ़ील्ड-विशिष्ट त्रुटि संदेश

यहाँ एक उदाहरण है कि मैं अनुकूलित करने के लिए चाहते हैं:

val form = Form(
    tuple("email" -> nonEmptyText, "password" -> nonEmptyText) 
     verifying ("Invalid email or password.", result => result match { 
     case (email, password) => { 
      User.authenticate(email, password).isDefined 
     } 
     })) 

बेहतर मेरी conf में/संदेशों फाइल मैं एक क्षेत्र विशिष्ट त्रुटि प्रदान कर सकता है:

error.email.required=Enter your login email address 
error.password.required=You must provide a password 

लेकिन सबसे खराब स्थिति में मैं फ़ील्ड नाम का उपयोग करके वाइल्डकार्ड संदेश से खुश होगा:

error.required=%s is required 
#would evaluate to "password is required", which I would then want to capitalize 

मैंने इस% s अभिव्यक्ति को देखा कुछ 1.x दस्तावेज चलाते हैं लेकिन यह अब काम नहीं कर रहा है।

आपकी मदद के लिए अग्रिम धन्यवाद!

उत्तर

8

कोशिश एक nonEmptyText के उपयोग ड्रॉप और एक कस्टम मान्यता के साथ एक सरल text क्षेत्र का उपयोग करें:

tuple(
    "email" -> text.verifying("Enter your login email address", {!_.isEmpty}), 
    "password" -> text.verifying("You must provide a password", {!_.isEmpty}) 
) 

फिर आप एक कदम और आगे ले जा सकते हैं और करने के लिए एक कॉल के लिए verifying खंड के अंदर String का आदान-प्रदान play.api.i18n.Messages वस्तु:

tuple(
    "email" -> text.verifying(Messages("error.email.required"), {!_.isEmpty}), 
    "password" -> text.verifying(Messages("error.password.required"), {!_.isEmpty}) 
) 

ध्यान दें कि यह अपरीक्षित कोड है, लेकिन यह दिशा का कहना है चाहिए।

गुड लक

+0

धन्यवाद @fynn मैं इस एक शॉट दे देंगे। – kgx

+0

आपका कोड बहुत अच्छा काम करता है। सही दिशा में मुझे इंगित करने के लिए फिर से धन्यवाद! – kgx

+0

नहीं, समस्या। मैं खुश हूं कि मैं मदद कर सका... – fynn

संबंधित मुद्दे