{"id":43076,"date":"2016-03-15T09:32:11","date_gmt":"2016-03-15T09:32:11","guid":{"rendered":"http:\/\/webkul.com\/blog\/?p=43076"},"modified":"2016-03-15T09:33:45","modified_gmt":"2016-03-15T09:33:45","slug":"symfony-validation-groups","status":"publish","type":"post","link":"https:\/\/webkul.com\/blog\/symfony-validation-groups\/","title":{"rendered":"Symfony Validation Groups"},"content":{"rendered":"<p><strong>Symfony Validation Groups &#8211;\u00a0<\/strong>In Symfony, normally we can validate user submitted values against our entities through these\u00a0ways &#8211; Annotation, Yaml, PHP etc. Example validation.yml for product entity<\/p>\n<pre class=\"brush:php\">Webkul\\DemoBundle\\Entity\\Product:\r\n    properties:\r\n        name:\r\n            - NotBlank: { message: \"product.name.not_blank\" }   \r\n            - Length:\r\n                max: 50\r\n                maxMessage: \"product.name.length\"\r\n        description:\r\n            - Length:\r\n                max: 200\r\n                maxMessage: \"product.description.length\"<\/pre>\n<p>But when a from or entity used more than one places to create form like\u00a0<span style=\"line-height: 1.5;\">Product in any e-commerce <\/span><\/p>\n<ul>\n<li><span style=\"line-height: 1.5;\">At time of product add you would never validate product id because it isn&#8217;t created yet.<\/span><\/li>\n<li>But at time of edit product, you do.<\/li>\n<\/ul>\n<p>Then how is form\/ entity going to tell Symfony\u00a0that\u00a0we need to validate id or not in current request ? For this we use <strong>Validation Groups\u00a0<\/strong>.\u00a0We declare validation groups in\u00a0our from and with properties declared in validation.yml.<\/p>\n<p>Controller (It will be array)<\/p>\n<pre class=\"brush:php\">$form = $this-&gt;createForm(new Product(), $product , array(\r\n                                    'validation_groups' =&gt; array('productEdit')\r\n                                   )\r\n                                 );\r\n<\/pre>\n<p>Yaml (Let&#8217;s say you added one validation group for\u00a0product create form &#8211; &#8220;product&#8221;)<\/p>\n<pre class=\"brush:php\">Webkul\\DemoBundle\\Entity\\Product:\r\n    properties:\r\n        productId: \r\n            - NotBlank: { groups: [productEdit] }\r\n        name:\r\n            - NotBlank: { groups: [product, productEdit] }   \r\n            - Length:\r\n                max: 50\r\n                maxMessage: \"product.name.length\"\r\n                groups: [product, productEdit]\r\n        description:\r\n            - Length:\r\n                max: 200\r\n                maxMessage: \"product.description.length\"\r\n                groups: [product, productEdit]\r\n<\/pre>\n<p>Now productId validation is enabled only for Product Edit form.<\/p>\n<p>Framework used \u2013 Symfony 2.7,\u00a0<a href=\"http:\/\/symfony.com\/doc\/current\/book\/validation.html#validation-groups\" target=\"_blank\">Official Doc<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Symfony Validation Groups &#8211;\u00a0In Symfony, normally we can validate user submitted values against our entities through these\u00a0ways &#8211; Annotation, Yaml, PHP etc. Example validation.yml for product entity Webkul\\DemoBundle\\Entity\\Product: properties: name: &#8211; NotBlank: { message: &#8220;product.name.not_blank&#8221; } &#8211; Length: max: 50 maxMessage: &#8220;product.name.length&#8221; description: &#8211; Length: max: 200 maxMessage: &#8220;product.description.length&#8221; But when a from or entity <a href=\"https:\/\/webkul.com\/blog\/symfony-validation-groups\/\">[&#8230;]<\/a><\/p>\n","protected":false},"author":14,"featured_media":43102,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,2707,2708,2858],"tags":[2710,131],"class_list":["post-43076","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","category-symfony","category-symfony2","category-validation","tag-symfony","tag-validation"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Symfony Validation Groups - Webkul Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/webkul.com\/blog\/symfony-validation-groups\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Symfony Validation Groups - Webkul Blog\" \/>\n<meta property=\"og:description\" content=\"Symfony Validation Groups &#8211;\u00a0In Symfony, normally we can validate user submitted values against our entities through these\u00a0ways &#8211; Annotation, Yaml, PHP etc. Example validation.yml for product entity WebkulDemoBundleEntityProduct: properties: name: - NotBlank: { message: &quot;product.name.not_blank&quot; } - Length: max: 50 maxMessage: &quot;product.name.length&quot; description: - Length: max: 200 maxMessage: &quot;product.description.length&quot; But when a from or entity [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/webkul.com\/blog\/symfony-validation-groups\/\" \/>\n<meta property=\"og:site_name\" content=\"Webkul Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webkul\/\" \/>\n<meta property=\"article:published_time\" content=\"2016-03-15T09:32:11+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2016-03-15T09:33:45+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png\" \/>\n\t<meta property=\"og:image:width\" content=\"825\" \/>\n\t<meta property=\"og:image:height\" content=\"260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Nikhil Malik\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webkul\" \/>\n<meta name=\"twitter:site\" content=\"@webkul\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nikhil Malik\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/webkul.com\/blog\/symfony-validation-groups\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/symfony-validation-groups\/\"},\"author\":{\"name\":\"Nikhil Malik\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/9a90e1b1a80142fc7445195c9ca8c8a4\"},\"headline\":\"Symfony Validation Groups\",\"datePublished\":\"2016-03-15T09:32:11+00:00\",\"dateModified\":\"2016-03-15T09:33:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/symfony-validation-groups\/\"},\"wordCount\":150,\"commentCount\":1,\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/symfony-validation-groups\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png\",\"keywords\":[\"symfony\",\"validation\"],\"articleSection\":[\"php\",\"Symfony\",\"Symfony2\",\"Validation\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/webkul.com\/blog\/symfony-validation-groups\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/webkul.com\/blog\/symfony-validation-groups\/\",\"url\":\"https:\/\/webkul.com\/blog\/symfony-validation-groups\/\",\"name\":\"Symfony Validation Groups - Webkul Blog\",\"isPartOf\":{\"@id\":\"https:\/\/webkul.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/webkul.com\/blog\/symfony-validation-groups\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/symfony-validation-groups\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png\",\"datePublished\":\"2016-03-15T09:32:11+00:00\",\"dateModified\":\"2016-03-15T09:33:45+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/webkul.com\/blog\/symfony-validation-groups\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/webkul.com\/blog\/symfony-validation-groups\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/symfony-validation-groups\/#primaryimage\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png\",\"width\":825,\"height\":260},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/webkul.com\/blog\/symfony-validation-groups\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/webkul.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Symfony Validation Groups\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/webkul.com\/blog\/#website\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"name\":\"Webkul Blog\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/webkul.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/webkul.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/webkul.com\/blog\/#organization\",\"name\":\"WebKul Software Private Limited\",\"url\":\"https:\/\/webkul.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"contentUrl\":\"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png\",\"width\":380,\"height\":380,\"caption\":\"WebKul Software Private Limited\"},\"image\":{\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webkul\/\",\"https:\/\/x.com\/webkul\",\"https:\/\/www.instagram.com\/webkul\/\",\"https:\/\/www.linkedin.com\/company\/webkul\",\"https:\/\/www.youtube.com\/user\/webkul\/\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/9a90e1b1a80142fc7445195c9ca8c8a4\",\"name\":\"Nikhil Malik\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/335b26b1a56ac61c8dcf3b6fa4c8c3d1e68ed57cd9db7e6f5dfa97cc506cf0d5?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/335b26b1a56ac61c8dcf3b6fa4c8c3d1e68ed57cd9db7e6f5dfa97cc506cf0d5?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g\",\"caption\":\"Nikhil Malik\"},\"url\":\"https:\/\/webkul.com\/blog\/author\/nikhil\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Symfony Validation Groups - Webkul Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/webkul.com\/blog\/symfony-validation-groups\/","og_locale":"en_US","og_type":"article","og_title":"Symfony Validation Groups - Webkul Blog","og_description":"Symfony Validation Groups &#8211;\u00a0In Symfony, normally we can validate user submitted values against our entities through these\u00a0ways &#8211; Annotation, Yaml, PHP etc. Example validation.yml for product entity WebkulDemoBundleEntityProduct: properties: name: - NotBlank: { message: \"product.name.not_blank\" } - Length: max: 50 maxMessage: \"product.name.length\" description: - Length: max: 200 maxMessage: \"product.description.length\" But when a from or entity [...]","og_url":"https:\/\/webkul.com\/blog\/symfony-validation-groups\/","og_site_name":"Webkul Blog","article_publisher":"https:\/\/www.facebook.com\/webkul\/","article_published_time":"2016-03-15T09:32:11+00:00","article_modified_time":"2016-03-15T09:33:45+00:00","og_image":[{"width":825,"height":260,"url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png","type":"image\/png"}],"author":"Nikhil Malik","twitter_card":"summary_large_image","twitter_creator":"@webkul","twitter_site":"@webkul","twitter_misc":{"Written by":"Nikhil Malik","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/webkul.com\/blog\/symfony-validation-groups\/#article","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/symfony-validation-groups\/"},"author":{"name":"Nikhil Malik","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/9a90e1b1a80142fc7445195c9ca8c8a4"},"headline":"Symfony Validation Groups","datePublished":"2016-03-15T09:32:11+00:00","dateModified":"2016-03-15T09:33:45+00:00","mainEntityOfPage":{"@id":"https:\/\/webkul.com\/blog\/symfony-validation-groups\/"},"wordCount":150,"commentCount":1,"publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"image":{"@id":"https:\/\/webkul.com\/blog\/symfony-validation-groups\/#primaryimage"},"thumbnailUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png","keywords":["symfony","validation"],"articleSection":["php","Symfony","Symfony2","Validation"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/webkul.com\/blog\/symfony-validation-groups\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/webkul.com\/blog\/symfony-validation-groups\/","url":"https:\/\/webkul.com\/blog\/symfony-validation-groups\/","name":"Symfony Validation Groups - Webkul Blog","isPartOf":{"@id":"https:\/\/webkul.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/webkul.com\/blog\/symfony-validation-groups\/#primaryimage"},"image":{"@id":"https:\/\/webkul.com\/blog\/symfony-validation-groups\/#primaryimage"},"thumbnailUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png","datePublished":"2016-03-15T09:32:11+00:00","dateModified":"2016-03-15T09:33:45+00:00","breadcrumb":{"@id":"https:\/\/webkul.com\/blog\/symfony-validation-groups\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/webkul.com\/blog\/symfony-validation-groups\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/symfony-validation-groups\/#primaryimage","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2016\/03\/Code-Snippet.png","width":825,"height":260},{"@type":"BreadcrumbList","@id":"https:\/\/webkul.com\/blog\/symfony-validation-groups\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/webkul.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Symfony Validation Groups"}]},{"@type":"WebSite","@id":"https:\/\/webkul.com\/blog\/#website","url":"https:\/\/webkul.com\/blog\/","name":"Webkul Blog","description":"","publisher":{"@id":"https:\/\/webkul.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/webkul.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/webkul.com\/blog\/#organization","name":"WebKul Software Private Limited","url":"https:\/\/webkul.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","contentUrl":"https:\/\/cdnblog.webkul.com\/blog\/wp-content\/uploads\/2021\/08\/webkul-logo-accent-sq.png","width":380,"height":380,"caption":"WebKul Software Private Limited"},"image":{"@id":"https:\/\/webkul.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webkul\/","https:\/\/x.com\/webkul","https:\/\/www.instagram.com\/webkul\/","https:\/\/www.linkedin.com\/company\/webkul","https:\/\/www.youtube.com\/user\/webkul\/"]},{"@type":"Person","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/9a90e1b1a80142fc7445195c9ca8c8a4","name":"Nikhil Malik","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/webkul.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/335b26b1a56ac61c8dcf3b6fa4c8c3d1e68ed57cd9db7e6f5dfa97cc506cf0d5?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/335b26b1a56ac61c8dcf3b6fa4c8c3d1e68ed57cd9db7e6f5dfa97cc506cf0d5?s=96&d=https%3A%2F%2Fcdnblog.webkul.com%2Fblog%2Fwp-content%2Fuploads%2F2019%2F10%2Fmike.png&r=g","caption":"Nikhil Malik"},"url":"https:\/\/webkul.com\/blog\/author\/nikhil\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/43076","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/comments?post=43076"}],"version-history":[{"count":6,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/43076\/revisions"}],"predecessor-version":[{"id":43109,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/posts\/43076\/revisions\/43109"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media\/43102"}],"wp:attachment":[{"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/media?parent=43076"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/categories?post=43076"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webkul.com\/blog\/wp-json\/wp\/v2\/tags?post=43076"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}