{"id":4055,"date":"2011-08-18T23:07:20","date_gmt":"2011-08-18T17:37:20","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=4055"},"modified":"2016-12-19T15:06:16","modified_gmt":"2016-12-19T09:36:16","slug":"inject-custom-validation-errors-in-object","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/inject-custom-validation-errors-in-object\/","title":{"rendered":"Inject custom validation errors in object"},"content":{"rendered":"<p>Reading the grails docs is like my habit, they always enhances your learning. Today I was going through the grails docs again and I found a very good way of showing some custom error messages. As I saw it I found the use case in one of my project that could be refactored by this approach<\/p>\n<p>There are use cases where we need to show some errors which are beyond your domain \/ command object  constraints validators for these cases you can inject your custom error messages to the object take an example of updating the password. Any of the field is not domain related so we have to create a command object like <\/p>\n<p>[java]<br \/>\nclass UpdatePasswordCO {<\/p>\n<p>    String password<br \/>\n    String newPassword<br \/>\n    String confirmNewPassword<\/p>\n<p>    static constraints = {<br \/>\n        password(nullable: false, blank: false)<br \/>\n        newPassword(nullable: false, blank: false)<br \/>\n        confirmNewPassword(nullable: false, blank: false, validator: {val,   obj -&gt;<br \/>\n            if (obj.newPassword != val) {<br \/>\n                &quot;confirmPassword.mismatch.newPassword&quot;<br \/>\n            }<br \/>\n        })<br \/>\n    }<br \/>\n}<br \/>\n[\/java]   <\/p>\n<p>Now you have to check whether the logged in user entered the correct password or not. So rather than putting the message in flash scope you can directly put this error to the command object it self<\/p>\n<p>[java]<br \/>\ndef updatePassword = {UpdatePasswordCO updatePasswordCO -&gt;<br \/>\n        if (updatePasswordCO.validate()) {<br \/>\n            if (User.loggedInUser.validatePassword(updatePasswordCO.password)) {<br \/>\n                flash.message = &quot;Password updated successfully&quot;<br \/>\n            } else {<br \/>\n                updatePasswordCO.errors.rejectValue(&quot;password&quot;, &quot;password.mismatch.current.password&quot;)<br \/>\n            }<br \/>\n        }<br \/>\n        if (updatePasswordCO.hasErrors()) {<br \/>\n            render(view: &#8216;profile&#8217;, model: [updatePasswordCO: updatePasswordCO])<br \/>\n        } else {<br \/>\n            redirect(action: &#8216;profile&#8217;)<br \/>\n        }<br \/>\n}<br \/>\n[\/java] <\/p>\n<p>So in above approach you have injected a new error on password field with a code. By doing this your hasErrors tag on gsp page will show your custom error as well. For more details you can refer <a href=\"http:\/\/static.springsource.org\/spring\/docs\/3.0.x\/javadoc-api\/org\/springframework\/validation\/Errors.html\">Spring<\/a><br \/>\n<\/p>\n<p>Hope it helps<br \/>\n<a href=\"http:\/\/www.tothenew.com\/blog\/author\/uday\/\">Uday Pratap Singh<\/a><br \/>\n<a href=\"mailto:uday@intelligrape.com\">uday@intelligrape.com<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Reading the grails docs is like my habit, they always enhances your learning. Today I was going through the grails docs again and I found a very good way of showing some custom error messages. As I saw it I found the use case in one of my project that could be refactored by this [&hellip;]<\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[7],"tags":[618,616,615,619,617],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4055"}],"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\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=4055"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/4055\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=4055"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=4055"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=4055"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}