{"id":6768,"date":"2012-08-30T15:21:40","date_gmt":"2012-08-30T09:51:40","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=6768"},"modified":"2012-10-04T11:37:59","modified_gmt":"2012-10-04T06:07:59","slug":"applying-validation-constraints-in-classes-other-than-domain-clases-and-command-objects","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/applying-validation-constraints-in-classes-other-than-domain-clases-and-command-objects\/","title":{"rendered":"Applying Validation Constraints in Classes other than Domain classes and Command Objects"},"content":{"rendered":"<p>We usually specify validation constraints in domain classes and command objects.<br \/>\nBut, if it is required to provide validation in other classes(like classes in src\/groovy), we can do so in two ways :<br \/>\n1. Defining a static constraints property in the class and using the @validateable annotation.<br \/>\n2. Defining a static constraints propery in the class and making an entry of the class in grails.validateable.classes property in config.groovy.  <\/p>\n<p>Here is an example of first way:<br \/>\n[java]<br \/>\npackage com.example.dto<br \/>\nimport grails.validation.Validateable<br \/>\n@Validateable<br \/>\nclass DataDTO {<br \/>\n\tString site<br \/>\n\tInteger views<\/p>\n<p>\tstatic constraints = {<br \/>\n\t\tsite blank: false<br \/>\n\t\tviews blank: false<br \/>\n\t}<br \/>\n}<br \/>\n[\/java]<br \/>\nIf a class is not marked with @Validateable, it may still be made validateable by defining the static constraints property in the class (as shown above) and then assigning class name(with package name) to the grails.validateable.classes property in Config.groovy:<\/p>\n<p>[java]<br \/>\ngrails.validateable.classes = [com.example.dto.DataDTO]<br \/>\n[\/java]<\/p>\n<p>In older versions of grails, one could achieve the same result in a slightly different way as mentioned <a href=\"http:\/\/www.tothenew.com\/blog\/grails-annotation-validateable\/\">here<\/a>. <\/p>\n<p>Hope it helps \ud83d\ude42<\/p>\n<p>Raj Gupta<br \/>\nraj.gupta@intelligrape.com<br \/>\n@rajdgreat007<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We usually specify validation constraints in domain classes and command objects. But, if it is required to provide validation in other classes(like classes in src\/groovy), we can do so in two ways : 1. Defining a static constraints property in the class and using the @validateable annotation. 2. Defining a static constraints propery in the [&hellip;]<\/p>\n","protected":false},"author":46,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":3},"categories":[7],"tags":[900,901],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6768"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/46"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=6768"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/6768\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=6768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=6768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=6768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}