7.4 Validation and Internationalization - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith
Version: null
7.4 Validation and Internationalization
Another important thing to note about errors in Grails is that error messages are not hard coded anywhere. The FieldError class in Spring resolves messages from message bundles using Grails' i18n support.
สิ่งสำคัญอีกอย่างนึงที่น่าจดจำไว้คือ ข้อความที่แสดงต่างๆใน Grails ไม่ได้ถูกกำหนดตายตัวไว้ในโค๊ดที่ไหน แต่คลาส FieldError ในสปริงเป็นตัวเรียกข้อความนั้นๆมาจาก message bundles โดยใช้ การสนับสนุนของ Grails' i18nConstraints and Message Codes
The codes themselves are dictated by a convention. For example consider the constraints we looked at earlier:
โดยโค๊ดนั้นถูกกำหนดไว้อยู่แล้ว เช่นจากตัวอย่างด้านล่างโดยมองจาก constraints แล้วpackage com.mycompany.myappclass User { ... static constraints = { login size: 5..15, blank: false, unique: true password size: 5..15, blank: false email email: true, blank: false age min: 18 } }
If a constraint is violated Grails will by convention look for a message code of the form:
ถ้า constraint ไม่เป็นไปตามกฏ Grails ก็จะเรียกข้อความมาจาก[Class Name].[Property Name].[Constraint Code]
In the case of the
โดยในตัวอย่างนี้ blank constraint this would be user.login.blank so you would need a message such as the following in your grails-app/i18n/messages.properties file:
blank constraint ก็จะเป็น user.login.blank โดยคุณก็จะต้องมีค่านี้ในไฟล์ grails-app/i18n/messages.properties ด้วยuser.login.blank=Your login name must be specified!
The class name is looked for both with and without a package, with the packaged version taking precedence. So for example, com.mycompany.myapp.User.login.blank will be used before user.login.blank. This allows for cases where your domain class message codes clash with a plugin's.
โดยชื่อของคลาสจะถูกค้นหาโดยมีแพกเกจหรือไม่มีแพกเกจก็ได้ แต่ ถ้ามีแพกเกจ Grails จะให้ความสำคัญมากกว่า เช่น com.mycompany.myapp.User.login.blank ก็จะถูกเรียกก่อน user.login.blank ทั้งนี้ทั้งนั้นเพื่อในกรณีที่ คลาสโดเมนมีชื่อตรงกับคลาสใดๆในปลั๊กอิน
For a reference on what codes are for which constraints refer to the reference guide for each constraint.
สำหรับการอ้างอิงว่าค่าใดใช้สำหรับ constraints ใดสามารถอ้างอิงได้จาก reference guide สำหรับแต่ล่ะ constraintDisplaying Messages
The renderErrors tag will automatically look up messages for you using the message tag. If you need more control of rendering you can handle this yourself:
renderErrors tag นี้ก็จะมองหา message tag ก่อนถ้าคุณต้องการความยืดหยุ่นมากกว่านั้นก็อาจจะกำหนดได้เองโดยตัวอย่างด้านล่าง<g:hasErrors bean="${user}"> <ul> <g:eachError var="err" bean="${user}"> <li><g:message error="${err}" /></li> </g:eachError> </ul> </g:hasErrors>
In this example within the body of the eachError tag we use the message tag in combination with its
ในตัวอย่างนี้ในบอดี้ของ eachError เราใช้ message tag และผสมผสานกับค่า error argument to read the message for the given error.
error เพื่ออ่านข้อความสำหรับแต่ล่ะข้อผิดพลาด

