{"id":49,"date":"2009-05-26T10:04:48","date_gmt":"2009-05-26T04:34:48","guid":{"rendered":"https:\/\/2thenew.online\/blog\/?p=49"},"modified":"2010-06-09T15:52:38","modified_gmt":"2010-06-09T10:22:38","slug":"command-objects","status":"publish","type":"post","link":"https:\/\/2thenew.online\/blog\/command-objects\/","title":{"rendered":"Command Objects"},"content":{"rendered":"<p><strong>What is a command object ?<br \/>\n<\/strong><br \/>\n Command object is a non persistent class used for data-validation. The main advantage of using the Command objects is that we can validate those forms that do not map to the domain class.<\/p>\n<p><strong>Where to define command object ?<\/strong><\/p>\n<p>You can define command classes in the <em>grails-app\/controllers<\/em> directory or even in the same file as a controller; or you may also define it in <em>grails-app\/src\/groovy.<\/em><\/p>\n<p><strong> How to define command object ?<br \/>\n<\/strong><br \/>\nIn one of my recent\u00a0 projects I did it like this :<\/p>\n<pre lang=\"groovy\">public class RegisterUserCommand {\r\n\r\n    String username\r\n    String password\r\n    String firstName\r\n    String lastName\r\n    String email\r\n    String age\r\n\r\n        static constraints={\r\n        username blank:false,size:30\r\n        password blank:false\r\n        cardNo creditCard:true\r\n        firstName matches\/[A-Za-z ]\/\r\n        lastName matches \/[A-Aa-z ]\/\r\n        email blank:false,email:true\r\n        age range:18..60\r\n    }\r\n\r\n}<\/pre>\n<p><strong>How to define custom validators ?<\/strong><\/p>\n<p>Lets us assume we &#8216;ve one more field as<em> verifyPassword <\/em>and we want to match it to the password field. We &#8216;ll use custom validator as :<\/p>\n<pre lang=\"groovy\">verifyPassword blank:false,validator:{val,obj ->\r\n        if(!(val==obj.password))\r\n        return ['field.match.message','Cofirm Password ','Password']\r\n}<\/pre>\n<p>Note :Here we &#8216;ve returned our own customized message instead of the default error message, along with the parameters.The <em>default error message<\/em> is implicitly returned when we use default validations.<\/p>\n<p><strong>How to use our command object now ?<\/strong><\/p>\n<p>When a request comes in, Grails will automatically create a new instance of the command object, bind the incoming request parameters to the properties of the instance, and pass it to you as the argument.<br \/>\nThus,you can create or modify your controller action as :<\/p>\n<pre lang=\"groovy\">def registerUser = { RegisterUserCommand cmd ->\r\n    if(!cmd.hasErrors()) {\r\n        \/\/ your code does something\r\n    }\r\n    else{\r\n            \/\/ your code does something\r\n    }\r\n}\r\n\r\n}<\/pre>\n<p><strong>How do I display error messages on my gsp page ?<br \/>\n<\/strong><br \/>\nTo do so you first need to pass the command object to your gsp page as :<\/p>\n<pre lang=\"groovy\">     render (view:'myErrorPage', model:[myCmd:cmd])<\/pre>\n<p> Then in the <em> myErrorPage.gsp <\/em>you can use g:renderErrors  tag to display the errors(if any). But before displaying errors u need to make sure there are errors by  making use of g:hasErrors tag :<\/p>\n<pre lang=\"groovy\">\r\n<g:hasErrors bean=\"${myCmd?.errors}\">\r\n          <div class=\"errors\">\r\n            <g:renderErrors bean=\"${cmd}\"\/>\r\n          <\/div>\r\n   <\/g:hasErrors>\r\n<\/pre>\n<p><strong>But it shows default error messages.How can I define my own error messages ?<br \/>\n<\/strong><br \/>\nTo do so you need to modify the<em> \/grails-app\/i18n\/message.properties_<\/em> file by creating your own messages or replacing the default messages by custom messages :<\/p>\n<pre lang=\"groovy\">\r\nfield.blank.message={0} cannot be blank\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #my message for blank fields\r\nfields.does.not.match.message={3} and {4} do not match\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # my verifyPassword error message\r\nfield.number.notallowed.message={3} must have alphabets only\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 # u can guess this<\/pre>\n<p><strong>I can&#8217;t follow the parameterized messages..can u please ellaborate.<br \/>\n<\/strong><br \/>\nThe error message\u00a0 is defined as:<\/p>\n<pre lang=\"groovy\">default.doesnt.match.message=Property [{0}] of class [{1}] with value [{2}] does not match the required pattern [{3}]<\/pre>\n<p>[{0}] = property<br \/>\n[{1}] = class<br \/>\n[{2}] = value<br \/>\n[{3}] = constraint<\/p>\n<p>The above example is a default message provided in the message.properties  file.One can easily modify the message to suit to the requirement.<br \/>\n Or you choose to define your own message.eg., for the example given in the begining , the error message for the <em>verifyPassword<\/em> validator can be defined as :<\/p>\n<pre lang=\"groovy\">fields.does.not.match.message={3} and {4} does not match<\/pre>\n<p> So in the validator the parameters and the message are returned as :<\/p>\n<pre lang=\"groovy\">return ['field.match.message','Cofirm Password ','Password']<\/pre>\n<p>Hope you find this useful.<br \/>\nImran Mir<br \/>\nimran@intelligrape.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is a command object ? Command object is a non persistent class used for data-validation. The main advantage of using the Command objects is that we can validate those forms that do not map to the domain class. Where to define command object ? You can define command classes in the grails-app\/controllers directory or [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":32,"footnotes":""},"categories":[7],"tags":[103,107,104],"class_list":["post-49","post","type-post","status-publish","format-standard","hentry","category-grails","tag-command-objects","tag-constraints","tag-messageproperties"],"aioseo_notices":[],"aioseo_head":"\n\t\t<!-- All in One SEO 5.0.0.1 - aioseo.com -->\n\t<meta name=\"description\" content=\"What is a command object ? Command object is a non persistent class used for data-validation. The main advantage of using the Command objects is that we can validate those forms that do not map to the domain class. Where to define command object ? You can define command classes in the grails-app\/controllers directory or\" \/>\n\t<meta name=\"robots\" content=\"max-image-preview:large\" \/>\n\t<meta name=\"author\" content=\"Imran Mir\"\/>\n\t<link rel=\"canonical\" href=\"https:\/\/2thenew.online\/blog\/command-objects\/\" \/>\n\t<meta name=\"generator\" content=\"All in One SEO (AIOSEO) 5.0.0.1\" \/>\n\t\t<meta property=\"og:locale\" content=\"en_US\" \/>\n\t\t<meta property=\"og:site_name\" content=\"TO THE NEW BLOG\" \/>\n\t\t<meta property=\"og:type\" content=\"blog\" \/>\n\t\t<meta property=\"og:title\" content=\"Command Objects | TO THE NEW Blog\" \/>\n\t\t<meta property=\"og:description\" content=\"What is a command object ? Command object is a non persistent class used for data-validation. The main advantage of using the Command objects is that we can validate those forms that do not map to the domain class. Where to define command object ? You can define command classes in the grails-app\/controllers directory or\" \/>\n\t\t<meta property=\"og:url\" content=\"https:\/\/2thenew.online\/blog\/command-objects\/\" \/>\n\t\t<meta property=\"og:image\" content=\"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta property=\"og:image:secure_url\" content=\"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<meta name=\"twitter:card\" content=\"summary\" \/>\n\t\t<meta name=\"twitter:site\" content=\"@tothenew\" \/>\n\t\t<meta name=\"twitter:title\" content=\"Command Objects | TO THE NEW Blog\" \/>\n\t\t<meta name=\"twitter:description\" content=\"What is a command object ? Command object is a non persistent class used for data-validation. The main advantage of using the Command objects is that we can validate those forms that do not map to the domain class. Where to define command object ? You can define command classes in the grails-app\/controllers directory or\" \/>\n\t\t<meta name=\"twitter:image\" content=\"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png\" \/>\n\t\t<script type=\"application\/ld+json\" class=\"aioseo-schema\">\n\t\t\t{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/command-objects\\\/#article\",\"name\":\"Command Objects | TO THE NEW Blog\",\"headline\":\"Command Objects\",\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/imran\\\/#author\"},\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"},\"datePublished\":\"2009-05-26T10:04:48+05:30\",\"dateModified\":\"2010-06-09T15:52:38+05:30\",\"inLanguage\":\"en-US\",\"commentCount\":7,\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/command-objects\\\/#webpage\"},\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/command-objects\\\/#webpage\"},\"articleSection\":\"Grails, Command Objects, constraints, message.properties\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/command-objects\\\/#breadcrumblist\",\"itemListElement\":[{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"name\":\"Grails\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"position\":2,\"name\":\"Grails\",\"item\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/\",\"nextItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/command-objects\\\/#listItem\",\"name\":\"Command Objects\"},\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog#listItem\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/command-objects\\\/#listItem\",\"position\":3,\"name\":\"Command Objects\",\"previousItem\":{\"@type\":\"ListItem\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/category\\\/grails\\\/#listItem\",\"name\":\"Grails\"}}]},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\",\"name\":\"TO THE NEW Blog\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/imran\\\/#author\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/imran\\\/\",\"name\":\"Imran Mir\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/command-objects\\\/#authorImage\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/186fa1c29a61225f60e52068be3f954d58ffa64750eac981d45154a72275b1eb?s=96&d=mm&r=g\",\"width\":96,\"height\":96,\"caption\":\"Imran Mir\"}},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/command-objects\\\/#webpage\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/command-objects\\\/\",\"name\":\"Command Objects | TO THE NEW Blog\",\"description\":\"What is a command object ? Command object is a non persistent class used for data-validation. The main advantage of using the Command objects is that we can validate those forms that do not map to the domain class. Where to define command object ? You can define command classes in the grails-app\\\/controllers directory or\",\"inLanguage\":\"en-US\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/command-objects\\\/#breadcrumblist\"},\"author\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/imran\\\/#author\"},\"creator\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/author\\\/imran\\\/#author\"},\"datePublished\":\"2009-05-26T10:04:48+05:30\",\"dateModified\":\"2010-06-09T15:52:38+05:30\"},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/\",\"name\":\"TO THE NEW Blog\",\"inLanguage\":\"en-US\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.tothenew.com\\\/blog\\\/#organization\"}}]}\n\t\t<\/script>\n\t\t<!-- All in One SEO -->\n\n","aioseo_head_json":{"title":"Command Objects | TO THE NEW Blog","description":"What is a command object ? Command object is a non persistent class used for data-validation. The main advantage of using the Command objects is that we can validate those forms that do not map to the domain class. Where to define command object ? You can define command classes in the grails-app\/controllers directory or","canonical_url":"https:\/\/2thenew.online\/blog\/command-objects\/","robots":"max-image-preview:large","keywords":"","webmasterTools":{"miscellaneous":""},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/2thenew.online\/blog\/command-objects\/#article","name":"Command Objects | TO THE NEW Blog","headline":"Command Objects","author":{"@id":"https:\/\/2thenew.online\/blog\/author\/imran\/#author"},"publisher":{"@id":"https:\/\/2thenew.online\/blog\/#organization"},"datePublished":"2009-05-26T10:04:48+05:30","dateModified":"2010-06-09T15:52:38+05:30","inLanguage":"en-US","commentCount":7,"mainEntityOfPage":{"@id":"https:\/\/2thenew.online\/blog\/command-objects\/#webpage"},"isPartOf":{"@id":"https:\/\/2thenew.online\/blog\/command-objects\/#webpage"},"articleSection":"Grails, Command Objects, constraints, message.properties"},{"@type":"BreadcrumbList","@id":"https:\/\/2thenew.online\/blog\/command-objects\/#breadcrumblist","itemListElement":[{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog#listItem","position":1,"name":"Home","item":"https:\/\/2thenew.online\/blog","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/category\/grails\/#listItem","name":"Grails"}},{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/category\/grails\/#listItem","position":2,"name":"Grails","item":"https:\/\/2thenew.online\/blog\/category\/grails\/","nextItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/command-objects\/#listItem","name":"Command Objects"},"previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog#listItem","name":"Home"}},{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/command-objects\/#listItem","position":3,"name":"Command Objects","previousItem":{"@type":"ListItem","@id":"https:\/\/2thenew.online\/blog\/category\/grails\/#listItem","name":"Grails"}}]},{"@type":"Organization","@id":"https:\/\/2thenew.online\/blog\/#organization","name":"TO THE NEW Blog","url":"https:\/\/2thenew.online\/blog\/"},{"@type":"Person","@id":"https:\/\/2thenew.online\/blog\/author\/imran\/#author","url":"https:\/\/2thenew.online\/blog\/author\/imran\/","name":"Imran Mir","image":{"@type":"ImageObject","@id":"https:\/\/2thenew.online\/blog\/command-objects\/#authorImage","url":"https:\/\/secure.gravatar.com\/avatar\/186fa1c29a61225f60e52068be3f954d58ffa64750eac981d45154a72275b1eb?s=96&d=mm&r=g","width":96,"height":96,"caption":"Imran Mir"}},{"@type":"WebPage","@id":"https:\/\/2thenew.online\/blog\/command-objects\/#webpage","url":"https:\/\/2thenew.online\/blog\/command-objects\/","name":"Command Objects | TO THE NEW Blog","description":"What is a command object ? Command object is a non persistent class used for data-validation. The main advantage of using the Command objects is that we can validate those forms that do not map to the domain class. Where to define command object ? You can define command classes in the grails-app\/controllers directory or","inLanguage":"en-US","isPartOf":{"@id":"https:\/\/2thenew.online\/blog\/#website"},"breadcrumb":{"@id":"https:\/\/2thenew.online\/blog\/command-objects\/#breadcrumblist"},"author":{"@id":"https:\/\/2thenew.online\/blog\/author\/imran\/#author"},"creator":{"@id":"https:\/\/2thenew.online\/blog\/author\/imran\/#author"},"datePublished":"2009-05-26T10:04:48+05:30","dateModified":"2010-06-09T15:52:38+05:30"},{"@type":"WebSite","@id":"https:\/\/2thenew.online\/blog\/#website","url":"https:\/\/2thenew.online\/blog\/","name":"TO THE NEW Blog","inLanguage":"en-US","publisher":{"@id":"https:\/\/2thenew.online\/blog\/#organization"}}]},"og:locale":"en_US","og:site_name":"TO THE NEW BLOG","og:type":"blog","og:title":"Command Objects | TO THE NEW Blog","og:description":"What is a command object ? Command object is a non persistent class used for data-validation. The main advantage of using the Command objects is that we can validate those forms that do not map to the domain class. Where to define command object ? You can define command classes in the grails-app\/controllers directory or","og:url":"https:\/\/2thenew.online\/blog\/command-objects\/","og:image":"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","og:image:secure_url":"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png","twitter:card":"summary","twitter:site":"@tothenew","twitter:title":"Command Objects | TO THE NEW Blog","twitter:description":"What is a command object ? Command object is a non persistent class used for data-validation. The main advantage of using the Command objects is that we can validate those forms that do not map to the domain class. Where to define command object ? You can define command classes in the grails-app\/controllers directory or","twitter:image":"https:\/\/2thenew.online\/blog\/wp-content\/themes\/ttn\/images\/social-logo.png"},"aioseo_meta_data":{"post_id":"49","title":null,"description":null,"keywords":null,"keyphrases":null,"primary_term":null,"canonical_url":null,"og_title":null,"og_description":null,"og_object_type":"default","og_image_type":"default","og_image_url":null,"og_image_width":null,"og_image_height":null,"og_image_custom_url":null,"og_image_custom_fields":null,"og_video":null,"og_custom_url":null,"og_article_section":null,"og_article_tags":null,"twitter_use_og":false,"twitter_card":"default","twitter_image_type":"default","twitter_image_url":null,"twitter_image_custom_url":null,"twitter_image_custom_fields":null,"twitter_title":null,"twitter_description":null,"schema":{"blockGraphs":[],"customGraphs":[],"default":{"data":{"Article":[],"Course":[],"Dataset":[],"FAQPage":[],"Movie":[],"Person":[],"Product":[],"ProductReview":[],"Car":[],"Recipe":[],"Service":[],"SoftwareApplication":[],"WebPage":[]},"graphName":"Article","isEnabled":true},"graphs":[]},"schema_type":null,"schema_type_options":null,"pillar_content":false,"robots_default":true,"robots_noindex":false,"robots_noarchive":false,"robots_nosnippet":false,"robots_nofollow":false,"robots_noimageindex":false,"robots_noodp":false,"robots_notranslate":false,"robots_max_snippet":null,"robots_max_videopreview":null,"robots_max_imagepreview":"large","priority":null,"frequency":null,"local_seo":null,"limit_modified_date":false,"created":"2021-04-30 05:49:10","updated":"2024-02-29 09:46:57","focus_keyword":null,"additional_keywords":null,"truseo_locale":null,"ai":null,"breadcrumb_settings":null,"seo_analyzer_scan_date":null},"aioseo_breadcrumb":"<div class=\"aioseo-breadcrumbs\"><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.online\/blog\" title=\"Home\">Home<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\t<a href=\"https:\/\/2thenew.online\/blog\/category\/grails\/\" title=\"Grails\">Grails<\/a>\n\t\t<\/span><span class=\"aioseo-breadcrumb-separator\">&raquo;<\/span><span class=\"aioseo-breadcrumb\">\n\t\t\tCommand Objects\n\t\t<\/span><\/div>","aioseo_breadcrumb_json":[{"label":"Home","link":"https:\/\/2thenew.online\/blog"},{"label":"Grails","link":"https:\/\/2thenew.online\/blog\/category\/grails\/"},{"label":"Command Objects","link":"https:\/\/2thenew.online\/blog\/command-objects\/"}],"_links":{"self":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/posts\/49","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/comments?post=49"}],"version-history":[{"count":0,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/posts\/49\/revisions"}],"wp:attachment":[{"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/media?parent=49"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/categories?post=49"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/2thenew.online\/blog\/wp-json\/wp\/v2\/tags?post=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}