////
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
////

link:GORM.html[Domain classes] and link:theWebLayer.html#commandObjects[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.


==== The Validateable Trait


Classes which define the static `constraints` property and implement the link:{api}grails/validation/Validateable.html[Validateable] trait will be validateable. Consider this example:

[source,groovy]
.src/main/groovy/com/mycompany/myapp/User.groovy
----
package com.mycompany.myapp

import grails.validation.Validateable

class User implements Validateable {
    ...

    static constraints = {
        login size: 5..15, blank: false, unique: true
        password size: 5..15, blank: false
        email email: true, blank: false
        age min: 18
    }
}
----

===== Programmatic access

Accessing the constraints on a validateable object is slightly different. You can access a command object's constraints programmatically in another context by accessing the `constraintsMap` static property of the class. That property is an instance of `Map<String, link:{api}grails/gorm/validation/ConstrainedProperty.html[ConstrainedProperty]>`

In the example above, accessing `User.constraintsMap.login.blank` would yield `false`, while
`User.constraintsMap.login.unique` would yield `true`.
