7.1 Declaring Constraints - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith
Version: null
7.1 Declaring Constraints
Within a domain class constraints are defined with the constraints property that is assigned a code block:
ในโดเมนคลาส constraints จะถูกกำหนดไว้ในค่าของ constraints ที่อยู่ในบล๊อกของโค๊ดclass User {
String login
String password
String email
Integer age static constraints = {
…
}
}
You then use method calls that match the property name for which the constraint applies in combination with named parameters to specify constraints:
โดยคุณก็จะสามารถเรียกใช้ method ที่ตรงกับชื่อของค่านั้นสำหรับแต่ล่ะ constraintclass 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
}
}
In this example we've declared that the
ในตัวอย่างนี้เราประกาศค่า login property must be between 5 and 15 characters long, it cannot be blank and must be unique. We've also applied other constraints to the password, email and age properties.
login ว่าจะต้องมีตัวอักษรระหว่าง 5 ถึง 15 โดยไม่สามารถเป็นค่าว่างได้และต้องเป็นค่าเฉพาะตัว เราก็ยังกำหนดค่าของ password, email และ age ด้วยBy default, all domain class properties are not nullable (i.e. they have an implicitโดยปกติแล้วค่าของโดเมนคลาสทั้งหมดจะไม่สามารถเป็นค่าว่างได้ โดยจะถูกกำหนดไว้nullable: falseconstraint). The same is not true for command object properties, which are nullable by default.nullable: falseconstraint โดยหลักการนี้จะไม่ตรงกับ command object เพราะแต่ล่ะค่าจะเป็นค่าว่างโดยปกติ
A complete reference for the available constraints can be found in the Quick Reference section under the Constraints heading.
โดยข้อมูลอ้างอิงโดยสมบูรณ์จะสามารถหาได้จาก Quick Reference ในหัวข้อ ConstraintsA word of warning - referencing domain class properties from constraints
It's very easy to attempt to reference instance variables from the static constraints block, but this isn't legal in Groovy (or Java). If you do so, you will get a
เป็นการง่ายมากที่จะลองใช้ค่าตัวแปร instance ในบล๊อกของ static constraints แต่นี้ไม่ถูกต้องตามหลักของ Groovy หรือแม้แต่ Java ถ้าคุณทำแบบนั้นคุณก็จะถูกแจ้งว่า MissingPropertyException for your trouble. For example, you may try
MissingPropertyException โดยดังเช่นตัวอย่างclass Response {
Survey survey
Answer answer static constraints = {
survey blank: false
answer blank: false, inList: survey.answers
}
}
See how the
โดยคุณจะเห็นได้ว่า inList constraint references the instance property survey? That won't work. Instead, use a custom validator:
inList constraint อ้างอิงกับค่าของ survey ซึ่งแบบนี้จะไม่ถูกต้องโดยเราจะต้องใช้ custom validator แทนclass Response {
…
static constraints = {
survey blank: false
answer blank: false, validator: { val, obj -> val in obj.survey.answers }
}
}
In this example, the
ในตัวอย่างนี้ ค่า obj argument to the custom validator is the domain instance that is being validated, so we can access its survey property and return a boolean to indicate whether the new value for the answer property, val, is valid.
obj เป็นค่าที่ถูกกำหนดขึ้นมาเองโดยเป็น instance ที่ถูกตรวจสอบ โดยเราสามารถเข้าถึง survey โดยการคืนค่า boolean เพื่อแสดงให้เห็นว่าค่า answer, val นั้นถูกต้องหรือเปล่า

