(Quick Reference)

7.5 Validation Non Domain and Command Object Classes - Reference Documentation

Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith

Version: null

7.5 Validation Non Domain and Command Object Classes

Domain classes and command objects support validation by default. Other classes may be made validateable by defining the static constraints property in the class (as described above) and then telling the framework about them. It is important that the application register the validateable classes with the framework. Simply defining the constraints property is not sufficient.

Domain classes และ command objects สนับสนุนการตรวจสอบข้อผิดพลาดโดยปกติอยู่แล้ว ถ้าสำหรับคลาสอื่นๆก็สามารถทำให้ตรวจสอบข้อผิดพลาดได้ โดยการกำหนดค่า static constraints ในคลาสนั้นๆ แล้วบอกกับ Grails ให้รับรู้ถึงคลาสนั้นๆ การบอกให้ Grails รับรู้ถึงการมีอยู่ของคลาสนั้นๆมีความสำคัญมาก โดยเพียงการกำหนด constraints นั้นไม่พอเพียง โดยจะเห็นได้จากตัวอย่างด้านล่าง

The Validateable Annotation

Classes which define the static constraints property and are annotated with @Validateable can be made validateable by the framework. Consider this example:

คลาสที่กำหนดค่า static constraints และประกาศ @Validateable ไว้ด้วยสามารถจะทำให้เป็นคลาสที่ตรวจสอบความผิดพลาดได้ โดยจะเห็นได้จากตัวอย่างด้านล่าง

// src/groovy/com/mycompany/myapp/User.groovy
package com.mycompany.myapp

import org.codehaus.groovy.grails.validation.Validateable

@Validateable class 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 } }

You tell the framework which packages to search for Validateable classes in by assigning a list of Strings to the grails.validateable.packages property in Config.groovy:

คุณต้องบอกกับ Grails ว่า แพกเกจในให้ทำการค้นหา Validateable โดยการกำหนดค่าสตริงใน grails.validateable.packages ใน Config.groovy:

grails.validateable.packages = ['com.mycompany.dto', 'com.mycompany.util']

The framework will only search those packages (and child packages of those) for classes annotated with @Validateable.

เฟรมเวิกจะทำการค้นหาเฉพาะในแพกเกจนั้นๆและ แพกเกจลูกโดยหาจาก @Validateable

Registering Validateable Classes

If a class is not marked with Validateable, it may still be made validateable by the framework. The steps required to do this are to define the static constraints property in the class (as described above) and then telling the framework about the class by assigning a value to the grails.validateable.classes property in Config.groovy@:

ถ้าคลาสนั้นไม่ได้ถูกกำหนดให้เป็น Validateable เราก็ยังสามารถทำให้เป็นคลาสที่ถูกตรวจสอบความผิดพลาดได้โดยเฟรมเวิก โดยนอกจากกำหนดค่า static constraints ในคลาสดั่งที่เห็นในตัวอย่างด้านบนแล้ว เราก็ยังต้องบอกกับเฟรมเวิกถึงการมีอยู่ของคลาสนั้นด้วยโดยการคำหนดค่า grails.validateable.classes ใน Config.groovy@:

grails.validateable.classes = [com.mycompany.myapp.User, com.mycompany.dto.Account]