{"id":133,"date":"2012-07-20T16:34:10","date_gmt":"2012-07-20T14:34:10","guid":{"rendered":"http:\/\/codingexplained.com\/?p=133"},"modified":"2017-06-11T22:27:52","modified_gmt":"2017-06-11T20:27:52","slug":"setting-an-elements-default-value","status":"publish","type":"post","link":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/setting-an-elements-default-value","title":{"rendered":"Setting an Element&#8217;s Default Value"},"content":{"rendered":"<p>In forms you will often want to set the default values of elements. This can easily be accomplished in Zend Framework by using the <span class=\"code\">setValue<\/span> method like so:<\/p>\n<pre><code class=\"php\">$select = new Zend_Form_Element_Select(&#039;myselect&#039;);\r\n$select-&gt;setMultiOptions(array(\r\n\t-1 =&gt; &#039;Gender&#039;,\r\n\t0 =&gt; &#039;Female&#039;,\r\n\t1 =&gt; &#039;Male&#039;\r\n))-&gt;setValue(1); \/\/ Set default to 1\r\n\t\t\t\t\r\n$this-&gt;addElement($select);<\/code><\/pre>\n<p>However, the above can also be accomplished by using the <span class=\"code\">setDefaults<\/span> method of the <span class=\"code\">Zend_Form<\/span> class by passing an array of element IDs and their desired default values. This leads us to the main purpose of this article. Logically, it would be feasible to think that this could be done like shown below.<\/p>\n<pre><code class=\"php\">$select = new Zend_Form_Element_Select(&#039;myselect&#039;);\r\n$select-&gt;setMultiOptions(array(\r\n\t-1 =&gt; &#039;Gender',\r\n\t0 =&gt; &#039;Female&#039;,\r\n\t1 =&gt; &#039;Male&#039;\r\n));\r\n\r\n\/\/ Set defaults for an array of elements. Key = element ID. Value = default value\r\n$this-&gt;setDefaults(array(\r\n\t&#039;myselect&#039; =&gt; 1\r\n));\r\n\t\t\t\t\r\n$this-&gt;addElement($select);<\/code><\/pre>\n<p>Surprisingly, <strong>this will not work!<\/strong>. This is because the <span class=\"code\">setDefaults<\/span> method call has to be <em>after<\/em> the element is added to the form. This is something that most people are likely to find illogical. Apparently the Zend Framework is very strict regarding the order of which statements are executed. It would probably make more sense if all of the elements&#8217; properties were added <em>before<\/em> the elements were added to the form.<\/p>\n<p>Instead, the below will work as intended.<\/p>\n<pre><code class=\"php\">$select = new Zend_Form_Element_Select(&#039;myselect&#039;);\r\n$select-&gt;setMultiOptions(array(\r\n\t-1 =&gt; &#039;Gender&#039;,\r\n\t0 =&gt; &#039;Female&#039;,\r\n\t1 =&gt; &#039;Male&#039;\r\n));\r\n\t\t\t\t\r\n$this-&gt;addElement($select);\r\n\r\n\/\/ Set defaults for an array of elements. Key = element ID. Value = default value\r\n$this-&gt;setDefaults(array(\r\n\t&#039;myselect&#039; =&gt; 1\r\n));<\/code><\/pre>\n<p>Hopefully this helps people who have faced this problem. Due to its illogical nature, it can be a quite difficult problem to solve as it is probably not the first thing a programmer considers to be wrong in this case.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It is not an unusual task to want to set the default values on elements in a form. Zend Framework provides multiple ways of accomplishing this, but as demonstrated in this article, the setDefaults method has surprising behavior.<\/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],"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>Setting an Element&#039;s Default Value<\/title>\n<meta name=\"description\" content=\"Using the setDefaults method in Zend Framework to set an element&#039;s default value may lead to unexpected problems. This article explains why.\" \/>\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\/setting-an-elements-default-value\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Setting an Element&#039;s Default Value\" \/>\n<meta property=\"og:description\" content=\"Using the setDefaults method in Zend Framework to set an element&#039;s default value may lead to unexpected problems. This article explains why.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/setting-an-elements-default-value\" \/>\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=\"2012-07-20T14:34:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-06-11T20:27:52+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\/setting-an-elements-default-value\",\"url\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/setting-an-elements-default-value\",\"name\":\"Setting an Element's Default Value\",\"isPartOf\":{\"@id\":\"https:\/\/codingexplained.com\/#website\"},\"datePublished\":\"2012-07-20T14:34:10+00:00\",\"dateModified\":\"2017-06-11T20:27:52+00:00\",\"author\":{\"@id\":\"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d\"},\"description\":\"Using the setDefaults method in Zend Framework to set an element's default value may lead to unexpected problems. This article explains why.\",\"breadcrumb\":{\"@id\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/setting-an-elements-default-value#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/setting-an-elements-default-value\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/setting-an-elements-default-value#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/codingexplained.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Setting an Element&#8217;s Default Value\"}]},{\"@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":"Setting an Element's Default Value","description":"Using the setDefaults method in Zend Framework to set an element's default value may lead to unexpected problems. This article explains why.","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\/setting-an-elements-default-value","og_locale":"en_US","og_type":"article","og_title":"Setting an Element's Default Value","og_description":"Using the setDefaults method in Zend Framework to set an element's default value may lead to unexpected problems. This article explains why.","og_url":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/setting-an-elements-default-value","og_site_name":"Coding Explained","article_publisher":"https:\/\/www.facebook.com\/codingexplained","article_author":"https:\/\/www.facebook.com\/codingexplained","article_published_time":"2012-07-20T14:34:10+00:00","article_modified_time":"2017-06-11T20:27:52+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\/setting-an-elements-default-value","url":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/setting-an-elements-default-value","name":"Setting an Element's Default Value","isPartOf":{"@id":"https:\/\/codingexplained.com\/#website"},"datePublished":"2012-07-20T14:34:10+00:00","dateModified":"2017-06-11T20:27:52+00:00","author":{"@id":"https:\/\/codingexplained.com\/#\/schema\/person\/e19c92ec991f571605f047cefeaa950d"},"description":"Using the setDefaults method in Zend Framework to set an element's default value may lead to unexpected problems. This article explains why.","breadcrumb":{"@id":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/setting-an-elements-default-value#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/codingexplained.com\/coding\/php\/zend-framework\/setting-an-elements-default-value"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/codingexplained.com\/coding\/php\/zend-framework\/setting-an-elements-default-value#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/codingexplained.com\/"},{"@type":"ListItem","position":2,"name":"Setting an Element&#8217;s Default Value"}]},{"@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-29","_links":{"self":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/133"}],"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=133"}],"version-history":[{"count":14,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/133\/revisions"}],"predecessor-version":[{"id":3054,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/posts\/133\/revisions\/3054"}],"wp:attachment":[{"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/media?parent=133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/categories?post=133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/tags?post=133"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/codingexplained.com\/wp-json\/wp\/v2\/series?post=133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}