{"id":532,"date":"2013-02-24T19:01:31","date_gmt":"2013-02-24T18:01:31","guid":{"rendered":"http:\/\/codingexplained.com\/?p=532"},"modified":"2017-06-11T22:12:43","modified_gmt":"2017-06-11T20:12:43","slug":"clear-form-data-in-zf2","status":"publish","type":"post","link":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/clear-form-data-in-zf2","title":{"rendered":"Clear Form Data in ZF2"},"content":{"rendered":"<p>Several people have asked how to clear a form in Zend Framework 2. In Zend Framework 1, there was the <span class=\"code\">reset<\/span> method on the <span class=\"code\">Zend_Form<\/span> class. In ZF2, one would think that the <span class=\"code\">populateValues<\/span> or <span class=\"code\">setData<\/span> methods would work when passing an empty array.<\/p>\n<p>However, as you might suspect, it does not. Instead, one can pass in an array of key\/value pairs; the key is the name of the element and the value is the element&#8217;s new value. If we wanted to clear the value of a <span class=\"code\">username<\/span> element, this could be accomplished with the code below.<\/p>\n<pre><code class=\"php\">$form->setData(array('username' => ''));<\/code><\/pre>\n<p>This is convenient if you want to reset the value of a single or a few elements. However, if you want to reset all elements, then it is not as convenient as in ZF1 because one has to add every form element to the array and maintain the correct element names according to the form class. An alternative is to loop through the form&#8217;s elements. Consider the code below.<\/p>\n<pre><code class=\"php\">$elements = $form->getElements();\r\n\r\nforeach ($elements as $element) {\r\n\tif ($element instanceof \\Zend\\Form\\Element\\Text) {\r\n\t\t$element->setValue('');\r\n\t}\r\n\r\n\t\/\/ Other element types here\r\n}<\/code><\/pre>\n<p>Above, we are looping through the form&#8217;s elements. The reason why there is a check for the type of element is because otherwise, a submit button&#8217;s value would be removed as well, for instance. This could of course be overwritten afterwards, but chances are that different form elements would have to be treated differently with this approach anyways.<\/p>\n<p>I have not spent much time trying to find better ways of doing this, so if you know of a more convenient way to go about it, please leave a comment. But if you have been stuck with this problem, then this will at least serve as a starting point. An alternative is to simply redirect back to the same page after form submission such that the form will be in its initial state (usually without data). A method for indicating which status message to display to the user can then be used, such as sessions (namely the FlashMessenger) or URL parameters. Otherwise simply instantiating the form anew is a viable option as per <a href=\"#comment-3562\" title=\"Clear all form data\">Mike Kelly&#8217;s comment<\/a>, although that does require you to add any dynamic data to the form again (e.g. data that is fetched from the database).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Clearing a form&#8217;s data was easy and convenient in Zend Framework 1. Apparently it is not as easy in Zend Framework 2, so this article gives a few suggestions on how it can be accomplished.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[38],"tags":[40,39,66],"series":[],"jetpack_publicize_connections":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Clear Form Data in ZF2<\/title>\n<meta name=\"description\" content=\"Learn how to clear a form&#039;s data in Zend Framework 2. While it is still easy, it is not as convenient as in Zend Framework 1.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/clear-form-data-in-zf2\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Clear Form Data in ZF2\" \/>\n<meta property=\"og:description\" content=\"Learn how to clear a form&#039;s data in Zend Framework 2. While it is still easy, it is not as convenient as in Zend Framework 1.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/clear-form-data-in-zf2\" \/>\n<meta property=\"og:site_name\" content=\"Coding Explained\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/codingexplained\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/codingexplained\" \/>\n<meta property=\"article:published_time\" content=\"2013-02-24T18:01:31+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-06-11T20:12:43+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/codingexplained.com\/wp-content\/uploads\/2015\/11\/codingexplained-fb-promote.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"444\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Bo Andersen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@codingexplained\" \/>\n<meta name=\"twitter:site\" content=\"@codingexplained\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bo Andersen\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/clear-form-data-in-zf2\",\"url\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/clear-form-data-in-zf2\",\"name\":\"Clear Form Data in ZF2\",\"isPartOf\":{\"@id\":\"https:\/\/codingexplained.com\/#website\"},\"datePublished\":\"2013-02-24T18:01:31+00:00\",\"dateModified\":\"2017-06-11T20:12:43+00:00\",\"author\":{\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d\"},\"description\":\"Learn how to clear a form's data in Zend Framework 2. While it is still easy, it is not as convenient as in Zend Framework 1.\",\"breadcrumb\":{\"@id\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/clear-form-data-in-zf2#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/clear-form-data-in-zf2\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/clear-form-data-in-zf2#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/codingexplained.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Clear Form Data in ZF2\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/codingexplained.com\/#website\",\"url\":\"https:\/\/codingexplained.com\/\",\"name\":\"Coding Explained\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/codingexplained.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d\",\"name\":\"Bo Andersen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g\",\"caption\":\"Bo Andersen\"},\"description\":\"I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!\",\"sameAs\":[\"https:\/\/codingexplained.com\",\"https:\/\/www.facebook.com\/codingexplained\",\"https:\/\/www.linkedin.com\/in\/ba0708\",\"https:\/\/twitter.com\/codingexplained\",\"https:\/\/www.youtube.com\/c\/codingexplained\"],\"url\":\"https:\/\/codingexplained.com\/author\/andy\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Clear Form Data in ZF2","description":"Learn how to clear a form's data in Zend Framework 2. While it is still easy, it is not as convenient as in Zend Framework 1.","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:\/\/codingexplained.com\/coding\/php\/zend-framework\/clear-form-data-in-zf2","og_locale":"en_US","og_type":"article","og_title":"Clear Form Data in ZF2","og_description":"Learn how to clear a form's data in Zend Framework 2. While it is still easy, it is not as convenient as in Zend Framework 1.","og_url":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/clear-form-data-in-zf2","og_site_name":"Coding Explained","article_publisher":"https:\/\/www.facebook.com\/codingexplained","article_author":"https:\/\/www.facebook.com\/codingexplained","article_published_time":"2013-02-24T18:01:31+00:00","article_modified_time":"2017-06-11T20:12:43+00:00","og_image":[{"width":1200,"height":444,"url":"https:\/\/codingexplained.com\/wp-content\/uploads\/2015\/11\/codingexplained-fb-promote.png","type":"image\/png"}],"author":"Bo Andersen","twitter_card":"summary_large_image","twitter_creator":"@codingexplained","twitter_site":"@codingexplained","twitter_misc":{"Written by":"Bo Andersen","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/clear-form-data-in-zf2","url":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/clear-form-data-in-zf2","name":"Clear Form Data in ZF2","isPartOf":{"@id":"https:\/\/codingexplained.com\/#website"},"datePublished":"2013-02-24T18:01:31+00:00","dateModified":"2017-06-11T20:12:43+00:00","author":{"@id":"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d"},"description":"Learn how to clear a form's data in Zend Framework 2. While it is still easy, it is not as convenient as in Zend Framework 1.","breadcrumb":{"@id":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/clear-form-data-in-zf2#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codingexplained.com\/coding\/php\/zend-framework\/clear-form-data-in-zf2"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/clear-form-data-in-zf2#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codingexplained.com\/"},{"@type":"ListItem","position":2,"name":"Clear Form Data in ZF2"}]},{"@type":"WebSite","@id":"https:\/\/codingexplained.com\/#website","url":"https:\/\/codingexplained.com\/","name":"Coding Explained","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/codingexplained.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d","name":"Bo Andersen","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/codingexplained.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/28f5826f9d5d544b0c5e1ec321dfdfb8?s=96&d=mm&r=g","caption":"Bo Andersen"},"description":"I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!","sameAs":["https:\/\/codingexplained.com","https:\/\/www.facebook.com\/codingexplained","https:\/\/www.linkedin.com\/in\/ba0708","https:\/\/twitter.com\/codingexplained","https:\/\/www.youtube.com\/c\/codingexplained"],"url":"https:\/\/codingexplained.com\/author\/andy"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p3mJkW-8A","_links":{"self":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/532"}],"collection":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/comments?post=532"}],"version-history":[{"count":9,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/532\/revisions"}],"predecessor-version":[{"id":3034,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/532\/revisions\/3034"}],"wp:attachment":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/media?parent=532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/categories?post=532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/tags?post=532"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/series?post=532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}