<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PavelDubinin.com</title>
	<atom:link href="http://paveldubinin.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://paveldubinin.com</link>
	<description>Professional Web Development</description>
	<lastBuildDate>Thu, 02 Feb 2012 10:14:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Machine learning gets closer &#8211; study how to create AI</title>
		<link>http://paveldubinin.com/2012/02/machine-learning-gets-closer/</link>
		<comments>http://paveldubinin.com/2012/02/machine-learning-gets-closer/#comments</comments>
		<pubDate>Thu, 02 Feb 2012 10:14:45 +0000</pubDate>
		<dc:creator>Pavel Dubinin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[AI]]></category>
		<category><![CDATA[machine learning]]></category>
		<category><![CDATA[statistics]]></category>

		<guid isPermaLink="false">http://paveldubinin.com/?p=350</guid>
		<description><![CDATA[Being a web developer was working good for me for around a decade now. I like the power of web, how it brings together multiple people, how I can see what various people do with what I&#8217;ve created, this is really inspiring. When social networks have appeared, it became even more interesting. I do not [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Machine+learning+gets+closer+%E2%80%93+study+how+to+create+AI+http%3A%2F%2Fpaveldubinin.com%2F2012%2F02%2Fmachine-learning-gets-closer%2F"><img class="nothumb" src="http://paveldubinin.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter6.png" alt="Post to Twitter" /></a></p></div><p><a href="http://paveldubinin.com/2012/02/machine-learning-gets-closer/three-dimensional-model-of-the-robot-with-books-and-the-diploma-2/" rel="attachment wp-att-353"><img class="wp-image-353 alignleft" title="Three-dimensional model of the robot with books and the diploma" src="http://paveldubinin.com/wp-content/uploads/mlclass1-243x300.jpg" alt="" width="243" height="300" /></a>Being a web developer was working good for me for around a decade now. I like the power of web, how it brings together multiple people, how I can see what various people do with what I&#8217;ve created, this is really inspiring.</p>
<p>When social networks have appeared, it became even more interesting. I do not spend too much time networking online, but I always liked how it works in terms of statistics. I like surprises coming from social networks. Like finding that my friend from one city works with my ex-classmate with whom we&#8217;ve been studing together at another city.</p>
<p>Social networks made styding people interations much easier. You can hold an experiment on large masses very fast. And alot of marketing programs now are based on this aspect.  So, that&#8217;s not only interesting, but also very profitable. People share their information themselves and this helps marketing guys create better and more focused products, so everyone&#8217;s happy.</p>
<p>So I found that statistics might be very interesting sometimes. As we get more and more data we need a way to process that efficiently. Guys from <a href="http://visual.ly/">visual.ly</a> made a step further by visualizing the data in a human-readable form.  I liked their feature where you can put in your twitter account and it will give some overview regarding you. It looks sooo nice.</p>
<p>Also, I was wondering on how google transalte manages to translate into that many languages. Well, maybe translations are not that perfect, but still it&#8217;s good to get basic understanding. I thought they must be hiring alot of translators on all various languages to prepare the database for all possible translations on all words.. crazy stuff. In fact, what happens to be is that google translator is taught automatically using machine learning algorithms. Alot of documents, texts are created in bulingular form and then machine is automatically taught to find which word means what on another language. Amazing!</p>
<p>It happens so that combining various ideas on mathematics, statistics, probability theory and others we can now let a computer do alot of things for analyzing data and even making a decision. That sounds like my child dream of making a real AI is getting closer and closer <img src='http://paveldubinin.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<p>The good thing is that I can start using it right now. Web applications need these sort of algorithms for everyday tasks. E.g. &#8220;recommended products&#8221; section from Amazon is based on what products you browsed, which commented, liked, tracing your path. And then it can give really good predictions on what you might like. And it is usally a very good fit, after &#8220;recommended books&#8221; newsletter they send, I frequently end up finding alot of books I&#8217;d like to read.</p>
<p>Or you can automatically find emotinal coloring of the comments on your blog. So that you can see in general what people think about your articles without reading a single comment.</p>
<p>From the economical side it can be very nice job of e.g. predicting of what the price of the product should be based on it&#8217;s characteristics, demand, competition. How nice. Don&#8217;t even get me started on the &#8220;VIP&#8221; techiques like image recognition,  voice recognition, speech synthesis etc..</p>
<p>These things really inspire me. Creating a program that can make decidions by itself, without being explicitly programmed, it a step towards really AI. We have a huge trip on this path, but what we already have is extremely impressive.</p>
<p>Earlier I thought that these algorithms are extremely hard to digest and I need to study AI at the university specifically, otherwise I have no chance. But I decided to give it a try anyway. What I found is that Standford university has sereval interesting video courses on <a href="http://www.ml-class.org/course/auth/welcome">machine learning</a> and <a href="www.nlp-class.org">natural language processing</a>, which are absolutely free. They are starting a new &#8220;semester&#8221; this February, so you can register to receive all the tasks, scripts etc. If you (like me) hate waiting, you can easily find video lectures from previous semesters on the web.</p>
<p>The way they&#8217;ve created these courses is very intuitive and clear, giving explanations in a good language and giving alot of examples, so it is easy to undestand. Math is also quite simple, for the advanced topics on math there are several optional lectures. So these courses are truly very good and I strongly recommend it.</p>
<p>I would like to network with people having that passion for AI, machine learning as I do to share the ideas and probably study together. If you&#8217;re interested in the topic, please share your thoughts about it. Maybe you know better sourses for studing or you&#8217;ve applied some machine learning techiques to your project? Or just as passionate about it as I am? I would love to hear!</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Machine+learning+gets+closer+%E2%80%93+study+how+to+create+AI+http%3A%2F%2Fpaveldubinin.com%2F2012%2F02%2Fmachine-learning-gets-closer%2F"><img class="nothumb" src="http://paveldubinin.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter6.png" alt="Post to Twitter" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://paveldubinin.com/2012/02/machine-learning-gets-closer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Keep yourself up-to-date!</title>
		<link>http://paveldubinin.com/2011/12/keep-yourself-up-to-date/</link>
		<comments>http://paveldubinin.com/2011/12/keep-yourself-up-to-date/#comments</comments>
		<pubDate>Fri, 16 Dec 2011 08:06:27 +0000</pubDate>
		<dc:creator>Pavel Dubinin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[books]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[testing]]></category>

		<guid isPermaLink="false">http://paveldubinin.com/?p=342</guid>
		<description><![CDATA[I believe that for programmers it is very important to constantly update their knowledge base. What was a cutting edge technology a couple years ago might already become outdated. The pace of our profession development is truly very fast, so being up-to-date is very important. There was a time I was working with a team [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Keep+yourself+up-to-date%21+http%3A%2F%2Fpaveldubinin.com%2F2011%2F12%2Fkeep-yourself-up-to-date%2F"><img class="nothumb" src="http://paveldubinin.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter6.png" alt="Post to Twitter" /></a></p></div><p>I believe that for programmers it is very important to constantly update their knowledge base. What was a cutting edge technology a couple years ago might already become outdated. The pace of our profession development is truly very fast, so being up-to-date is very important.</p>
<p>There was a time I was working with a team developing accounting software for (mostly goverment) companies. All of the software was written for DOS and old developers had a strong resistance of moving to something new. Their reasoning was that everyone is using it anyway and that it takes time to teach an accountant a new technology. But what I really saw is that they just don&#8217;t know how to develop software for Windows and honestly I had a feeling that they don&#8217;t want to learn anything. They were ok with the current state of things when they just do what they have been doing for 20+ years.</p>
<p>But the times has changed, noone wanted their software developed for DOS anymore, everyone wanted to have a nice modern interface and usable interaction using mouse when they can just click whatever they needed to see the details. So these guys has become outdated very fast.</p>
<p>Moreover, the code they produced was procedural-stlye of course, full of tricks, global variables everywhere and very very hard to undestand. One small change would require alot of hours (or even days) to accomplish without being sure that it does not break anything else.</p>
<p>Needless to say that this company had very hard time&#8230; Old guys could not do what the market wanted, new guys did not want to work with them because they saw that resistance from old members and prefered to go work somewhere else.</p>
<p>Very quickly I left that company also. When seeing that situation happening I told to myself that whatever happends I should never become like this! I don&#8217;t want to do the same thing again and again util retired. I want to learn something new all the time, because learning something new is not only rewarding, but also very fun.</p>
<p>So now I keep reading the book one after another on various topics because this what fills a brain with new ideas. I might agree or disagree with the author, but in either case it makes my brain work, it makes me want experiment with things and do things better.</p>
<p>Funny enough is to see how most of people (particularly girls) react when seeing me reading the book. Well, you know alot of programming books are quite thick. And you know people think that anyone who reads the thick book (or wears glasses) must be very clever. It does not even matter that I&#8217;ve just started reading it and have no idea of what it is all about <img src='http://paveldubinin.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>One of the books I&#8217;m reading now <em>Sebastian Bergmann, Stefan Priebsch &#8211; Real-World Solutio</em><a href="http://www.amazon.com/gp/product/0470872497/ref=as_li_tf_il?ie=UTF8&amp;tag=profewebdevel-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0470872497"><img class="alignright" style="border: 0pt none;" src="http://ws.assoc-amazon.com/widgets/q?_encoding=UTF8&amp;Format=_SL160_&amp;ASIN=0470872497&amp;MarketPlace=US&amp;ID=AsinImage&amp;WS=1&amp;tag=profewebdevel-20&amp;ServiceVersion=20070822" alt="" width="147" height="181" border="0" /></a><em>ns for Developing High-Quality PHP Frameworks and Applications &#8211; 2011</em> (click on the image at the right for details if interested)<em>.</em></p>
<p><em> </em>Really an enjoying reading, it systemizes the concepts of  developing enterprise-style applications and gives alot of tips and examples about developing very clean and testable code with examples<em>. </em>This was particularly useful for me because I started to digg deeper the topic of  automated testing to start applying it more systematically to my projects.<em><br />
</em></p>
<p>I&#8217;m v<img style="border: none !important; margin: 0px !important;" src="http://www.assoc-amazon.com/e/ir?t=profewebdevel-20&amp;l=as2&amp;o=1&amp;a=0470872497" alt="" width="1" height="1" border="0" />ery excited on what I&#8217;ve learnt about the topic so far, so I will probably blog more about that soon..</p>
<p>What about you? Do you read often? Which books helped you grow as a professional? What are you reading now? I&#8217;d really love to hear from you at the comments section!</p>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Keep+yourself+up-to-date%21+http%3A%2F%2Fpaveldubinin.com%2F2011%2F12%2Fkeep-yourself-up-to-date%2F"><img class="nothumb" src="http://paveldubinin.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter6.png" alt="Post to Twitter" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://paveldubinin.com/2011/12/keep-yourself-up-to-date/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Custom validation with Zend Form</title>
		<link>http://paveldubinin.com/2011/06/custom-validation-with-zend-form/</link>
		<comments>http://paveldubinin.com/2011/06/custom-validation-with-zend-form/#comments</comments>
		<pubDate>Wed, 08 Jun 2011 04:32:01 +0000</pubDate>
		<dc:creator>Pavel Dubinin</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[Zend Form]]></category>

		<guid isPermaLink="false">http://paveldubinin.com/?p=306</guid>
		<description><![CDATA[Today I want to show you a simple, but a very usable concept of validating a form depending on some criteria. There was a time when for one of my projects I needed to create a dynamic form similar to this one: When checkbox near the field is checked, the field needs to be required, [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Custom+validation+with+Zend+Form+http%3A%2F%2Fpaveldubinin.com%2F2011%2F06%2Fcustom-validation-with-zend-form%2F"><img class="nothumb" src="http://paveldubinin.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter6.png" alt="Post to Twitter" /></a></p></div><p>Today I want to show you a simple, but a very usable concept of validating a form depending on some criteria. There was a time when for one of my projects I needed to create a dynamic form similar to this one:</p>
<p><a rel="attachment wp-att-307" href="http://paveldubinin.com/2011/06/custom-validation-with-zend-form/custom-validation/"><a rel="attachment wp-att-308" href="http://paveldubinin.com/2011/06/custom-validation-with-zend-form/custom-validation-2/"><img class="alignnone size-full wp-image-308" title="custom-validation" src="http://paveldubinin.com/wp-content/uploads/custom-validation1.png" alt="Custom Validation" width="271" height="171" /></a></a></p>
<p>When checkbox near the field is checked, the field needs to be required, otherwise we just ignore it.</p>
<p>Usually people solve tasks like this by writting alot of code at their controllers, but as I wrote at <a title="Thin Clever Beautiful Controllers" href="http://paveldubinin.com/2011/05/thin-clever-beautiful-controllers/" target="_blank">my previous post</a> this is not the best way to do things.  We want to keep our controller as clean as possible and validation task is a task for the form, not for the controller.</p>
<p>But for the cases like one described above we cannot just assign one of the default validators to a form like we do usually. Writting custom validator also does not solve the problem as we need to validate form depending on criteria based on several fields.</p>
<p>So the best way to solve this in my opinion is to override isValid() method at your custom form. Let me first give you the entire code for the form and then I will explain the details below:</p>
<pre class="brush: php; title: ; notranslate">
class Default_Form_CustomValidation extends Zend_Form
{
	public function init()
	{
		$this-&gt;setOptions(array(
			'elements' =&gt; array(
				'field_a_enabled' =&gt; array(
					'type' =&gt; 'checkbox',
				),
				'field_a' =&gt; array(
					'type' =&gt; 'text',
					'options' =&gt; array(
						'label' =&gt; 'Field A',
					)
				),

				'field_b_enabled' =&gt; array(
					'type' =&gt; 'checkbox',
				),
				'field_b' =&gt; array(
					'type' =&gt; 'text',
					'options' =&gt; array(
						'label' =&gt; 'Field B',
					)
				),

				'field_c_enabled' =&gt; array(
					'type' =&gt; 'checkbox',
				),
				'field_c' =&gt; array(
					'type' =&gt; 'text',
					'options' =&gt; array(
						'label' =&gt; 'Field C',
					)
				),

				'submit' =&gt; array(
					'type' =&gt; 'submit',
					'options' =&gt; array(
						'label' =&gt; 'Save',
						'decorators' =&gt;	array('ViewHelper')
					),
				),

			),

			'decorators' =&gt; array('FormElements', 'Form'),
			'elementDecorators' =&gt; array('Label', 'ViewHelper', 'Errors')
		));

		$this-&gt;addDisplayGroups(array(
			'a' =&gt; array('elements' =&gt; array('field_a_enabled', 'field_a')),
			'b' =&gt; array('elements' =&gt; array('field_b_enabled', 'field_b')),
			'c' =&gt; array('elements' =&gt; array('field_c_enabled', 'field_c')),
			's' =&gt; array('elements' =&gt; array('submit'))
		));

		$this-&gt;setDisplayGroupDecorators(array('FormElements', array('HtmlTag', array('tag'=&gt;'div'))));
	}

	public function isValid($data)
	{
		$valid_values = $this-&gt;getValidValues($data, true);

		if ($valid_values['field_a_enabled']==='1') {
			$this-&gt;field_a-&gt;setRequired(true);
		} else {
			$this-&gt;field_a-&gt;setIgnore(true);
		}

		if ($valid_values['field_b_enabled']==='1') {
			$this-&gt;field_b-&gt;setRequired(true);
		} else {

			$this-&gt;field_b-&gt;setIgnore(true);
		}

		if ($valid_values['field_c_enabled']==='1') {
			$this-&gt;field_c-&gt;setRequired(true);
		} else {
			$this-&gt;field_c-&gt;setIgnore(true);
		}

		$this-&gt;field_a_enabled-&gt;setIgnore(true);
		$this-&gt;field_b_enabled-&gt;setIgnore(true);
		$this-&gt;field_c_enabled-&gt;setIgnore(true);

		return parent::isValid($data);
	}
}
</pre>
<p>First, at the init() function we define the fields we need and group it to let it look the way we want. As you can see, each checkbox/input pair is placed into a separate group and each group has &#8220;HtmlTag&#8221; decorator which wraps them into &#8220;div&#8221; tag.<br />
This allows us to have checkbox and input placed together, but each new set of those to be placed in a new row.</p>
<p>But the magic of course happens at isValid() function. There I start with this line:</p>
<pre class="brush: php; title: ; notranslate">
		$valid_values = $this-&gt;getValidValues($data, true);
</pre>
<p>getValidValues() validates the form and retieves only the values that passed validation.<br />
At this specific example this line might not seem too useful, and you could use $data array directly to access values, but this is a good practice to always use getValidValues() to retrieve validated values, as it goes through the standard form validation routine for each element here and also filters all the input data from the user, so this adds up to the security of your application.</p>
<p>Ok, so when received a list of valid values, we need to make our custom validation. This is as simple as this for each of our fields:</p>
<pre class="brush: php; title: ; notranslate">
		if ($valid_values['field_a_enabled']==='1') {
			$this-&gt;field_a-&gt;setRequired(true);
		} else {
			$this-&gt;field_a-&gt;setIgnore(true);
		}
</pre>
<p>What this code says to our application is simply that if a checkbox (field_a_enabled) was checked, then input (field_a) needs to be required, otherwise we just skip it.</p>
<p>For those of you who don&#8217;t know what setIgnore() function actually does I will make some explanation as I really like that one. This is quite simple, at your controller when you retrieve your validated values with $form->getValues(true), fields which were marked as ignored do not go to the resulting &#8220;values array&#8221; regardless to whether POST sent any values for them or not. So this helps to keep things clean and don&#8217;t mess up with any values that we don&#8217;t need anyway.</p>
<p>What it means in our case is that we&#8217;ll only get values for the input fields which were checked with corresponding checkboxes, nothing else. Controller does not need to know anything about checkboxes and it does not need to know about inputs which were not selected (corresponding checkbox not checked). This is the job for the form, and controller will only receive the data it really needs &#8211; values from the selected inputs.</p>
<p>When done with custom validation we also set all checkboxes to ignore as they already did their job of letting validation know which fields are required and which are not and we don&#8217;t need them further:</p>
<pre class="brush: php; title: ; notranslate">
		$this-&gt;field_a_enabled-&gt;setIgnore(true);
		$this-&gt;field_b_enabled-&gt;setIgnore(true);
		$this-&gt;field_c_enabled-&gt;setIgnore(true);
</pre>
<p>And last, but very importaint line is this:</p>
<pre class="brush: php; title: ; notranslate">
		return parent::isValid($data);
</pre>
<p>With this line we actually call Zend&#8217;s native validation from Zend_Form class as our version of isValid() does not actually validate anything, it just prepares certain fields to be validated by native validation.</p>
<p>So that&#8217;s it! That was just an example of what you can do by overriding isValid() and writting custom validation, this gives you a great power to create any really dynamic and flexible validation.</p>
<p>Hope that was useful and I could convince you that placing your custom validation into the form class instead of controller makes your code cleaner and easy to understand.</p>
<p>And as usual I would be glad to hear your thoughts at the comments section.<br />
Thanks!</p>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Custom+validation+with+Zend+Form+http%3A%2F%2Fpaveldubinin.com%2F2011%2F06%2Fcustom-validation-with-zend-form%2F"><img class="nothumb" src="http://paveldubinin.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter6.png" alt="Post to Twitter" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://paveldubinin.com/2011/06/custom-validation-with-zend-form/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Thin Clever Beautiful Controllers</title>
		<link>http://paveldubinin.com/2011/05/thin-clever-beautiful-controllers/</link>
		<comments>http://paveldubinin.com/2011/05/thin-clever-beautiful-controllers/#comments</comments>
		<pubDate>Sat, 21 May 2011 07:55:54 +0000</pubDate>
		<dc:creator>Pavel Dubinin</dc:creator>
				<category><![CDATA[Generic]]></category>
		<category><![CDATA[Controllers]]></category>
		<category><![CDATA[MVC]]></category>
		<category><![CDATA[oop]]></category>

		<guid isPermaLink="false">http://paveldubinin.com/?p=282</guid>
		<description><![CDATA[Very often I see how much people overload their controllers. That situation is so much frequent that it even received it&#8217;s own term &#8220;Fat Stupid Ugly Controllers&#8221;. So what does that mean? If you just have 3 components of MVC &#8211; model, view and controller &#8211; this is not enough to claim that your application [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Thin+Clever+Beautiful+Controllers+http%3A%2F%2Fpaveldubinin.com%2F2011%2F05%2Fthin-clever-beautiful-controllers%2F"><img class="nothumb" src="http://paveldubinin.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter6.png" alt="Post to Twitter" /></a></p></div><p>Very often I see how much people overload their controllers. That situation is so much frequent that it even received it&#8217;s own term &#8220;<em>Fat Stupid</em> Ugly Controllers&#8221;. So what does that mean? If you just have 3 components of MVC &#8211; model, view and controller &#8211; this is not enough to claim that your application really follows MVC paradigm.</p>
<p>Controller needs to be a very lightweight and thin part of your application. Afterall, all it needs to do is to receive some data and pass it to the model, then receive proccessed data back from model and send to view.  So it would be <em>&#8220;Thin Clever Beautiful Controllers&#8221; </em>instead <img src='http://paveldubinin.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>The most common mistake is putting business logic inside the controller. It not only makes your code harder to understand, but also reduces chances to reuse your code. Sometimes I spot that people don&#8217;t really understand MVC by asking questions like this:</p>
<blockquote><p>How do I <em>include</em> code from one controller into another controller?</p></blockquote>
<p>You should never even want to do that if you use MVC propertly. All code which is to be reused at different controllers needs to be at your model. From the other hand, sometimes all you need is to <em>forward</em> your request into another controller.</p>
<p>In general, the concept of OOP programming is to create as small chunks of code as possible. The entire application is divided into very small peaces, each of which is easy to digest to anyone who reads it (and by the way, studies showed that code is read 10 times  more often than it&#8217;s written).</p>
<p>Keeping more code at your model (rather than at the controller) also helps with testing, as controllers are almost impossible to test with automated tools. Models, from the other hand, are easy to test with tools like PHPUnit or similar.</p>
<p>I saw similar thing happening with OOP and people claiming that they know how to use it. But it was nothing more than  the same old procedural approach with a set of different functions which are unrelated to each other and just grouped under one class. So they thought that they&#8217;re using OOP and that&#8217;s cool and technically they were, but in reality it was just a disguised procedural programming.</p>
<p>So look at your code once again and think &#8211; are you really using technology you claim to be using? Dig deeper and find what is inside. That is the only way to grow as a professional&#8230;</p>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Thin+Clever+Beautiful+Controllers+http%3A%2F%2Fpaveldubinin.com%2F2011%2F05%2Fthin-clever-beautiful-controllers%2F"><img class="nothumb" src="http://paveldubinin.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter6.png" alt="Post to Twitter" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://paveldubinin.com/2011/05/thin-clever-beautiful-controllers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>2 columns form with Zend Form</title>
		<link>http://paveldubinin.com/2011/04/2-columns-form-with-zend-form/</link>
		<comments>http://paveldubinin.com/2011/04/2-columns-form-with-zend-form/#comments</comments>
		<pubDate>Sat, 23 Apr 2011 08:45:53 +0000</pubDate>
		<dc:creator>Pavel Dubinin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[2 columns form]]></category>
		<category><![CDATA[multicolumn form]]></category>
		<category><![CDATA[Zend Form]]></category>

		<guid isPermaLink="false">http://paveldubinin.com/?p=265</guid>
		<description><![CDATA[Here&#8217;s yet another post about Zend Form. I love the power of Zend Forms, but there are many tricks I had to learn to make it work the way I want to. Very often people ask how to create a form which has several columns. At this post I want to share how you can [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=2+columns+form+with+Zend+Form+http%3A%2F%2Fpaveldubinin.com%2F2011%2F04%2F2-columns-form-with-zend-form%2F"><img class="nothumb" src="http://paveldubinin.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter6.png" alt="Post to Twitter" /></a></p></div><p>Here&#8217;s yet another post about Zend Form. I love the power of Zend Forms, but there are many tricks I had to learn to make it work the way I want to.</p>
<p><a href="http://paveldubinin.com/wp-content/uploads/two-column-form.png"><img class="alignnone size-medium wp-image-271" title="two-column-form" src="http://paveldubinin.com/wp-content/uploads/two-column-form-300x186.png" alt="Two column form" width="300" height="186" /></a></p>
<p>Very often people ask how to create a form which has several columns. At this post I want to share how you can do that, giving 2 columns form as an example. But you can create multicolumn form with any number of columns you want using the same technique.</p>
<p>Ok, lets just start with some simple form definition:</p>
<pre class="brush: php; title: ; notranslate">
class Default_Form_TwoColumn extends Zend_Form
{
	public function init()
	{
		$this-&gt;setOptions(array(
			'elements' =&gt; array(
				'element1' =&gt; array(
					'type' =&gt; 'text',
					'options' =&gt; array(
						'label' =&gt; 'Element 1',
					)
				),
				'element2' =&gt; array(
					'type' =&gt; 'text',
					'options' =&gt; array(
						'label' =&gt; 'Element 2',
					)
				),
				'element3' =&gt; array(
					'type' =&gt; 'text',
					'options' =&gt; array(
						'label' =&gt; 'Element 3',
					)
				),
				'element4' =&gt; array(
					'type' =&gt; 'text',
					'options' =&gt; array(
						'label' =&gt; 'Element 4',
					)
				),
				'element5' =&gt; array(
					'type' =&gt; 'text',
					'options' =&gt; array(
						'label' =&gt; 'Element 5',
					)
				),
				'element6' =&gt; array(
					'type' =&gt; 'text',
					'options' =&gt; array(
						'label' =&gt; 'Element 6',
					)
				),
				'submit' =&gt; array(
					'type' =&gt; 'submit',
					'options' =&gt; array(
						'label' =&gt; 'Save'
					)
				)
			),
		));

	}
}
</pre>
<p>What I do here is simply add 6 input elements and one submit button. They will all show one under another in one column. Ok, but we need 2 columns. An easy way to archive this  is using display groups. What display groups do is simply grouping elements together.</p>
<p>Here we&#8217;ll need 3 display groups &#8211; first for the left column, second for the right column and third for our submit button:</p>
<pre class="brush: php; title: ; notranslate">
		$this-&gt;addDisplayGroups(array(
			'left' =&gt; array(
				'options'  =&gt; array('description' =&gt; 'Left Column'),
				'elements' =&gt; array('element1', 'element2', 'element3'),
			),
			'right' =&gt; array(
				'options'  =&gt; array('description' =&gt; 'Right Column'),
				'elements' =&gt; array('element4', 'element5', 'element6'),
			),
			'bottom' =&gt; array(
				'elements' =&gt; array('submit'),
			)
		));

		$this-&gt;setDisplayGroupDecorators(array('Description', 'FormElements', 'Fieldset'));
</pre>
<p>At this point you should see something like this:</p>
<p><a href="http://paveldubinin.com/wp-content/uploads/grouped-elements.png"><img class="alignnone size-medium wp-image-274" title="grouped-elements" src="http://paveldubinin.com/wp-content/uploads/grouped-elements-284x300.png" alt="Grouped elements" width="284" height="300" /></a></p>
<p>That border around our elements clearly shows what we have in our display groups. That border is displayed because of the &#8220;fieldset&#8221; tag added around our elements. By default each display group just wraps a set of its elements in a &#8220;fieldset&#8221; tag, but you can apply any other decorators to them.</p>
<p>You can also notice that I used setDisplayGroupDecorators() here. That is to override default decorators which were adding some tags that I did not need, also I added &#8220;Description&#8221; decorator so that to let it show the &#8220;title&#8221; of the column at the top.</p>
<p>Alright, this is all nice, but we still have one column form. What else should we do? Well, not everything needs to be solved at the server side. Sometimes all you need is just adding some styles with css:</p>
<pre class="brush: css; title: ; notranslate">
				form fieldset#fieldset-left, form fieldset#fieldset-right
				{
					float: left;
					width: 15%;
					border: none;
				}

				form #fieldset-bottom
				{
					clear: both;
					border: none;
				}

				form dd
				{
					margin: 0;
				}

				form fieldset .hint
				{
					font-weight: bold;
				}
</pre>
<p>Here I set floting left for our main columns and clear:both for submit button. As you can see I used ids #fieldset-left, #fieldset-right, #fieldset-bottom &#8211; these ids are  automatically added by display groups and named after name of the display group, so that way that&#8217;s easy to style separate groups using css.<br />
I also added some more styling just to let our form look a little nicer.</p>
<p><a href="http://paveldubinin.com/wp-content/uploads/two-column-form.png"><img class="alignnone size-medium wp-image-271" title="two-column-form" src="http://paveldubinin.com/wp-content/uploads/two-column-form-300x186.png" alt="Two column form" width="300" height="186" /></a></p>
<p>Ok, that&#8217;s it! We&#8217;ve created a nice two column form with Zend Framework. If you need more columns, just add more display groups, place your elements into them and make sure you add &#8220;float: left&#8221; style to your display groups.</p>
<p>Hope that was helpful for you. Feel free to ask any questions at the comments section.</p>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=2+columns+form+with+Zend+Form+http%3A%2F%2Fpaveldubinin.com%2F2011%2F04%2F2-columns-form-with-zend-form%2F"><img class="nothumb" src="http://paveldubinin.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter6.png" alt="Post to Twitter" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://paveldubinin.com/2011/04/2-columns-form-with-zend-form/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Custom Zend Element &#8211; Checkbox Tree</title>
		<link>http://paveldubinin.com/2011/04/custom-zend-element-checkbox-tree/</link>
		<comments>http://paveldubinin.com/2011/04/custom-zend-element-checkbox-tree/#comments</comments>
		<pubDate>Sat, 16 Apr 2011 07:52:06 +0000</pubDate>
		<dc:creator>Pavel Dubinin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[checkbox]]></category>
		<category><![CDATA[tree]]></category>
		<category><![CDATA[Zend Form]]></category>

		<guid isPermaLink="false">http://paveldubinin.com/?p=200</guid>
		<description><![CDATA[Alright, tuning and decorating elements is nice and fun. But what if you need something really custom like a checkbox tree? In this case you will need to create a custom Zend Element. It might sound complicated at the beginning, but this is actually quite simple. I prefer this appoach rather than using ViewScript to [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Custom+Zend+Element+%E2%80%93+Checkbox+Tree+http%3A%2F%2Fpaveldubinin.com%2F2011%2F04%2Fcustom-zend-element-checkbox-tree%2F"><img class="nothumb" src="http://paveldubinin.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter6.png" alt="Post to Twitter" /></a></p></div><p>Alright, tuning and decorating elements is nice and fun. But what if you need something really custom like a checkbox tree? In this case you will need to create a custom Zend Element. It might sound complicated at the beginning, but this is actually quite simple.</p>
<p><a href="http://paveldubinin.com/wp-content/uploads/treeview.png"><img class="alignnone size-medium wp-image-201" title="treeview" src="http://paveldubinin.com/wp-content/uploads/treeview-187x300.png" alt="" width="187" height="300" /></a></p>
<p>I prefer this appoach rather than using ViewScript to create what you  need because you can make it really reusable and also attach validation  and displaying errors to it.</p>
<p>If you are eager to try this right now,  you can <strong>download a complete project with example <a title="Checkbox Tree" href="http://paveldubinin.com/?download=Zend%20Checkbox%20Tree&#038;n=1">here</a></strong>. The element itself can be found at library/ZFExt folder inside that zip file.</p>
<h3>Usage</h3>
<h4>Form</h4>
<p>applicaitons/forms/Tree.php</p>
<p>Here&#8217;s a simple example of the form using checkbox tree element (same as on screenshot above):</p>
<pre class="brush: php; title: ; notranslate">
class Default_Form_Tree extends Zend_Form
{
	public function init()
	{
		$this-&gt;addPrefixPath('ZFExt_Form_Element', 'ZFExt/Form/Element/', 'Element');

		$this-&gt;setOptions(array(
			'elements' =&gt; array(
				'tree' =&gt; array(
					'type' =&gt; 'treeView',
					'options' =&gt; array(
						'label' =&gt; 'Tree:',
						'multioptions' =&gt; array(
							1 =&gt; array('title'=&gt;'Node 1', 'children'=&gt;array()),
							2 =&gt; array('title'=&gt;'Node 2', 'children'=&gt;array(
								21 =&gt; array('title'=&gt;'Node 2.1', 'children'=&gt;array()),
								22 =&gt; array('title'=&gt;'Node 2.2', 'children'=&gt;array(
									221 =&gt; array('title'=&gt;'Node 2.2.1', 'children'=&gt;array()),
									222 =&gt; array('title'=&gt;'Node 2.2.2', 'children'=&gt;array()),
									223 =&gt; array('title'=&gt;'Node 2.2.3', 'children'=&gt;array()),
								)),
							)),
							3 =&gt; array('title'=&gt;'Node 3', 'children'=&gt;array()),
							4 =&gt; array('title'=&gt;'Node 4', 'children'=&gt;array(
								41 =&gt; array('title'=&gt;'Node 4.1', 'children'=&gt;array()),
								42 =&gt; array('title'=&gt;'Node 4.2', 'children'=&gt;array(
									421 =&gt; array('title'=&gt;'Node 4.2.1', 'children'=&gt;array()),
									422 =&gt; array('title'=&gt;'Node 4.2.2', 'children'=&gt;array()),
								)),
							)),
							5 =&gt; array('title'=&gt;'Node 5', 'children'=&gt;array()),
						)
					)
				),
				'submit' =&gt; array(
					'type' =&gt; 'submit',
					'options' =&gt; array(
						'ignore' =&gt; true,
						'label' =&gt; 'Submit',
						'class' =&gt; 'submit',
					)
				),
			),
		));

	}
}
</pre>
<p>We pass hierarchy using &#8220;multioptions&#8221; parameter, which is an array with the following structure:</p>
<blockquote><p><em>$id =&gt; array(&#8216;title&#8217;=&gt;$title, &#8216;children&#8217;=&gt;array())</em></p></blockquote>
<p><em>$id</em> is an unique ID defining each node, <em>$title</em> is a  caption for each node, children array is a list of child nodes for each  node. So nesting nodes into &#8220;children&#8221; array will create a hierarchy.</p>
<h4>Controller</h4>
<p>application/modules/default/controllers/IndexController.php</p>
<pre class="brush: php; title: ; notranslate">
class IndexController extends Zend_Controller_Action {

	public function indexAction()
	{
		$this-&gt;view-&gt;addHelperPath('ZFExt/View/Helper', 'ZFExt_View_Helper');

		$form = new Default_Form_Tree();

		if ($this-&gt;_request-&gt;isPost()) {
			if ($form-&gt;isValid($_POST)) {
				$values = $form-&gt;getValues(true);

				var_dump($values);
			}
		}

		$this-&gt;view-&gt;form = $form;
	}
}
</pre>
<p>Nothing fancy here, just going through standard validation routine.</p>
<h4>View</h4>
<p>application/modules/default/views/scripts/index/index.phtml</p>
<pre class="brush: xml; title: ; notranslate">
&lt;?php echo $this-&gt;form; ?&gt;

&lt;script src=&quot;http://code.jquery.com/jquery-latest.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;http://jquery.bassistance.de/treeview/lib/jquery.cookie.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;

&lt;link rel=&quot;stylesheet&quot; href=&quot;http://jquery.bassistance.de/treeview/demo/screen.css&quot; type=&quot;text/css&quot; /&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;http://jquery.bassistance.de/treeview/jquery.treeview.css&quot; type=&quot;text/css&quot; /&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;http://jquery.bassistance.de/treeview/jquery.treeview.js&quot;&gt;&lt;/script&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
$(document).ready(function(){
	$(&quot;#tree&quot;).treeview({
		persist: &quot;cookie&quot;,
		cookieId: &quot;treeview-black&quot;
	});
});
&lt;/script&gt;
</pre>
<p>Here we output our form + add some javascript to make our tree expandable/collapsable. I used scripts from here <a href="http://bassistance.de/jquery-plugins/">http://bassistance.de/jquery-plugins/</a></p>
<h3>Implementation Details</h3>
<p>Creating custom control consists on two parts &#8211; creating element class and creating helper class.</p>
<h4>Element</h4>
<p>Element class is what you add to your form (same as &#8220;text&#8221; or &#8220;checkbox&#8221; element or whatever else). It handles all the validation, population of the value to the element, assigning options etc.</p>
<p>Here&#8217;s how our element class looks like:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
class ZFExt_Form_Element_TreeView extends Zend_Form_Element_Multi
{
	public $helper = 'formTreeView';

    /**
     * MultiCheckbox is an array of values by default
     * @var bool
     */
    protected $_isArray = true;

	public function _getOptionIds($options)
	{
		$result = array();
		foreach ($options as $current_option_id=&gt;$current_option) {
			$result[] = $current_option_id;
			if (isset($current_option['children'])) {
				$result = array_merge($result, $this-&gt;_getOptionIds($current_option['children']));
			}
		}
		return $result;
	}

    public function isValid($value, $context = null)
    {
        if ($this-&gt;registerInArrayValidator()) {
            if (!$this-&gt;getValidator('InArray')) {
                $multi_options = $this-&gt;getMultiOptions();

				$option_ids = $this-&gt;_getOptionIds($multi_options);
                $this-&gt;addValidator(
                    'InArray',
                    true,
                    array($option_ids)
                );
            }
        }
        return parent::isValid($value, $context);
    }

}
</pre>
<p>As you can see, we extend it from Zend_Form_Element_Multi which is a base class for all elements containing multiple options (like select or radiogroup etc.) Obviously with checkbox tree we have multiple options (checkboxes), so Zend_Form_Element_Multi suits our needs nicely.</p>
<p><em>$helper = &#8216;formTreeView&#8217;</em> tells which helper we want to use to draw this element. We will discuss helper a little later.</p>
<p>Also we set value for <em>$_isArray</em> to true, telling that this element returns array as a value, we use it because we allow to check multiple checkboxes, so result will obviously be an array.</p>
<p>At  <em>isValid()</em> function we add a default validator (InArray). The purpose of it is to check that values submitted to the form are in the tree. In other words, so that it is impossible to submit values which are not amoung the options of the element. So for this purpose we collect ids of all options of the element and pass it to the default &#8220;InArray&#8221; validator.</p>
<h4>Helper</h4>
<p>All the &#8220;magic&#8221; happends at the helper class. That one is responsible for rendering element itself i.e. creating HTML for the checkbox tree. Let&#8217;s have a look at the code:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
class ZFExt_View_Helper_FormTreeView extends Zend_View_Helper_FormElement
{
	protected $_info = null;

	protected function _renderBranch($nodes)
	{
		$output = '&lt;ul id=&quot;'.$this-&gt;_info['id'].'&quot;'.$this-&gt;_htmlAttribs($this-&gt;_info['attribs']).'&gt;';
			$id = $this-&gt;_info['id'];

			foreach ($nodes as $node_id=&gt;$node) {
				$node_control_id = $id.'-'.$node_id;
				$node_control_name = $this-&gt;_info['name'];

				if (is_array($this-&gt;_info['value'])) {
					$checked = in_array($node_id, $this-&gt;_info['value'])?'checked=&quot;checked&quot;':'';
				} else {
					$checked = '';
				}

				$output .= '&lt;li&gt;';
					$output .= '&lt;input '.$checked.' value=&quot;'.$node_id.'&quot; id=&quot;'.$node_control_id.'&quot; name=&quot;'.$node_control_name.'&quot; type=&quot;checkbox&quot; /&gt;';
					$output .= '&lt;label for=&quot;'.$node_control_id.'&quot;&gt;'.$node['title'].'&lt;/label&gt;';
					if (!empty($node['children'])) {
						$output	.= $this-&gt;_renderBranch($node['children']);
					}
				$output .= '&lt;/li&gt;';
			}
		$output .= '&lt;/ul&gt;';

		return $output;
	}

	public function formTreeView ($name, $value = null, $attribs = null, $options = null)
	{
		$this-&gt;_info = $this-&gt;_getInfo($name, $value, $attribs, $options);
		return $this-&gt;_renderBranch($this-&gt;_info['options']);
	}
}
</pre>
<p>It starts from <em>formTreeView()</em> method and then <a title="Recursion" href="http://en.wikipedia.org/wiki/Recursion" target="_blank">recursively</a> runs <em>_renderBranch()</em> on each &#8220;children&#8221; subarray to build a tree. So, each node is wrapped in &#8220;&lt;li&gt;&lt;/li&gt;&#8221; tags and contains a checkbox and a label for that checkbox (for the title of the node). If node has some children, they are added into another &#8220;ul&#8221; element under this node.</p>
<p><em>_renderBranch()</em> also checks for the value passed to element (populated after submit), so that it knows which checkboxes need to be checked and which not:</p>
<pre class="brush: php; title: ; notranslate">$checked = in_array($node_id, $this-&gt;_info['value'])?'checked=&quot;checked&quot;':'';</pre>
<p>So that&#8217;s what our helper renders as a result:</p>
<p><a href="http://paveldubinin.com/wp-content/uploads/treeview-scriptless.png"><img class="alignnone size-full wp-image-236" title="treeview-scriptless" src="http://paveldubinin.com/wp-content/uploads/treeview-scriptless.png" alt="" width="198" height="269" /></a></p>
<p>Now what&#8217;s left is just adding some scripts to make that tree dynamic:</p>
<pre class="brush: xml; title: ; notranslate">
&lt;script src=&quot;http://code.jquery.com/jquery-latest.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;http://jquery.bassistance.de/treeview/lib/jquery.cookie.js&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;

&lt;link rel=&quot;stylesheet&quot; href=&quot;http://jquery.bassistance.de/treeview/demo/screen.css&quot; type=&quot;text/css&quot; /&gt;
&lt;link rel=&quot;stylesheet&quot; href=&quot;http://jquery.bassistance.de/treeview/jquery.treeview.css&quot; type=&quot;text/css&quot; /&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;http://jquery.bassistance.de/treeview/jquery.treeview.js&quot;&gt;&lt;/script&gt;

&lt;script type=&quot;text/javascript&quot;&gt;
$(document).ready(function(){
	$(&quot;#tree&quot;).treeview({
		persist: &quot;cookie&quot;,
		cookieId: &quot;treeview-black&quot;
	});
});
&lt;/script&gt;
</pre>
<p>I&#8217;ve chosen to use jquery script from here <a href="http://bassistance.de/jquery-plugins/">http://bassistance.de/jquery-plugins/</a> but you can use any script of your choice.</p>
<p>Please notice that I connect it with &#8220;#tree&#8221; element. That&#8217;s because my element at the form is called &#8220;tree&#8221; and HTML id for it was auto-generated. If you have different name, you will also need to update your script accordingly.</p>
<p>Ok, so that&#8217;s it! You can find all sources for this article <a title="Checkbox Tree" href="http://paveldubinin.com/?download=Zend%20Checkbox%20Tree&#038;n=2">here</a>.</p>
<p>Inside is a zend project along with minified version of zend framework (so that you could try it without installing anything else).  The most interesting part is at library/ZFExt folder, that&#8217;s were our element and helper reside.</p>
<p>Thanks for reading! If you have any questions &#8211; don&#8217;t hesitate to ask using comments section.</p>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Custom+Zend+Element+%E2%80%93+Checkbox+Tree+http%3A%2F%2Fpaveldubinin.com%2F2011%2F04%2Fcustom-zend-element-checkbox-tree%2F"><img class="nothumb" src="http://paveldubinin.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter6.png" alt="Post to Twitter" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://paveldubinin.com/2011/04/custom-zend-element-checkbox-tree/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Why people avoid standards?</title>
		<link>http://paveldubinin.com/2011/04/why-people-avoid-standards/</link>
		<comments>http://paveldubinin.com/2011/04/why-people-avoid-standards/#comments</comments>
		<pubDate>Wed, 13 Apr 2011 10:26:18 +0000</pubDate>
		<dc:creator>Pavel Dubinin</dc:creator>
				<category><![CDATA[Freelancing]]></category>
		<category><![CDATA[freelance]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[standards]]></category>

		<guid isPermaLink="false">http://paveldubinin.com/?p=192</guid>
		<description><![CDATA[There was a time I needed to outsource some of my work, so I started looking for a coder that could help me with XHTML/CSS part.  I checked few portfolios which people were sending to me, but most of the websites I saw there where sliced using tables. When I asked one of these guys [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Why+people+avoid+standards%3F+http%3A%2F%2Fpaveldubinin.com%2F2011%2F04%2Fwhy-people-avoid-standards%2F"><img class="nothumb" src="http://paveldubinin.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter6.png" alt="Post to Twitter" /></a></p></div><p>There was a time I needed to outsource some of my work, so I started looking for a coder that could help me with XHTML/CSS part.  I checked few portfolios which people were sending to me, but most of the websites I saw there where sliced using tables. When I asked one of these guys if he had something to show using &#8220;div-style&#8221; markup he replied:</p>
<blockquote><p>&#8220;No! I&#8217;m not going to follow the standards created by some geeks working for food&#8221;</p></blockquote>
<p>He meant W3C standards. First  I thought that it was just him having such a crazy thinking. But then I started asking more and got very similar replies. Some people mentioned that w3c-style markup is much harder to create with and that they don&#8217;t want to waste time on that or that certain layouts are impossible to create just using CSS.</p>
<p>Ok, that could be an excuse sometime at the 1990&#8242;s, but not today! With XHTML/CSS layout you can make so much more things and so much more easier and cleaner, so I&#8217;m shocked that people still use table layouts for anything at all. Clean markup also affects SEO positively, makes your page load faster, displays nicely on different devices etc etc.</p>
<p>Another day I talked to one programmer. He says:</p>
<blockquote><p>&#8220;All this <abbr title="Object Oriented Programming">OOP</abbr> stuff is crazy, I can do everything much faster using functional programming. And yeah, objects take so much RAM, so I try to avoid them all the time&#8221;</p></blockquote>
<p>Come on, OOP was invented more than 20 years ago, since that time it has evolved, many modern languages are based on it&#8217;s priciples, there are thousands of books, hundrends of reusable patterns etc.</p>
<p>That reminded me conversation with these HTML-guys. What was in common with all these guys that they avoided to use proven technology, thinking that they go &#8220;the right way&#8221;? They even think that they&#8217;re &#8220;cool&#8221; as they don&#8217;t follow the rules and the rest of the world are just fools.</p>
<p>Interestingly, as I found out, most of these guys are beginners. So it&#8217;s rediculous when that newbie which was into the programming for 1-2 years and created a couple of messy websites (if any at all) says that technology evolution for several decades was crap.</p>
<p>I don&#8217;t want to say that everyone has to follow the path of the world&#8217;s &#8220;big guys&#8221;. Innovation is great and as we could see very often, revolutions in technology appear all the time. But, I believe that it is only possible with this path:</p>
<ol>
<li>Learn technology</li>
<li>Find it&#8217;s weaknesses</li>
<li>Create something better</li>
</ol>
<p>Most people just go straight to point 2 or point 3.  They just avoid learning, they just put tags &#8220;complicated&#8221;, &#8220;slow&#8221; etc. without even trying it. I mean really trying it. Writting something more than a simple &#8220;Hello world&#8221;. They forget that:</p>
<blockquote>
<p style="text-align: center;">&#8220;Chance favors the prepared mind.&#8221;<br />
&#8211;  <strong>Louis Pasteur</strong></p>
</blockquote>
<p>It&#8217;s like someone who first tried to ride the bicycle saying &#8220;Bicycle is  so hard to  learn riding, it costs money and it breaks sometimes. I can  just walk and do it much faster&#8221;. Well, it might be true at the beginning, but when you&#8217;ve learnt how to ride it, you understand how funny your ideas were.</p>
<p>If you find yourself blaming some technology think about it once again&#8230; Maybe that kind of thinking blocks your growth as a professional and you miss something really important?</p>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Why+people+avoid+standards%3F+http%3A%2F%2Fpaveldubinin.com%2F2011%2F04%2Fwhy-people-avoid-standards%2F"><img class="nothumb" src="http://paveldubinin.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter6.png" alt="Post to Twitter" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://paveldubinin.com/2011/04/why-people-avoid-standards/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Real table based form with Zend Form</title>
		<link>http://paveldubinin.com/2011/04/real-table-based-form-with-zend-form/</link>
		<comments>http://paveldubinin.com/2011/04/real-table-based-form-with-zend-form/#comments</comments>
		<pubDate>Thu, 07 Apr 2011 16:54:34 +0000</pubDate>
		<dc:creator>Pavel Dubinin</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[table]]></category>
		<category><![CDATA[Zend Form]]></category>

		<guid isPermaLink="false">http://paveldubinin.com/?p=172</guid>
		<description><![CDATA[As you might have already noticed from my previous posts, when it comes to form creation I try to avoid coding manual HTML as much as possible. Afterall, we have all that nice elements and decorators available at Zend Framework, so why wasting it? So, I have one more thing from my Zend Form&#8217;s toolset [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Real+table+based+form+with+Zend+Form+http%3A%2F%2Fpaveldubinin.com%2F2011%2F04%2Freal-table-based-form-with-zend-form%2F"><img class="nothumb" src="http://paveldubinin.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter6.png" alt="Post to Twitter" /></a></p></div><p>As you might have already noticed from my previous posts, when it comes to form creation I try to avoid coding manual HTML as much as possible. Afterall, we have all that nice elements and decorators available at Zend Framework, so why wasting it?</p>
<p>So, I have one more thing from my Zend Form&#8217;s toolset to share with you. At this post I want to create a real table based form with Zend Form. What I mean is this:</p>
<p><a href="http://paveldubinin.com/wp-content/tableform.png"><img class="alignnone size-medium wp-image-186" title="tableform" src="http://paveldubinin.com/wp-content/tableform.png" alt="Table-style Zend Form" /></a></p>
<p>A table, but still a form and no custom viewscript. If you want solution right away, you can download the entire code for this post <a href="http://paveldubinin.com/?download=Table_Form">here</a></p>
<p>Otherwise, see how it is done step by step:</p>
<p><strong>1. Start with a simple form having just a &#8220;submit&#8221; button:</strong></p>
<pre class="brush: php; title: ; notranslate">
class Default_Form_SimpleTable extends Zend_Form
{
	public function init()
	{
		$this-&gt;setOptions(array(
			'elements' =&gt; array(
				'submit' =&gt; array(
					'type' =&gt; 'submit',
				)
			),
		));
	}
}
</pre>
<p><strong>2. Add table decorators for our main form.</strong></p>
<pre class="brush: php; title: ; notranslate">
$this-&gt;setDecorators(array('FormElements', array('SimpleTable', array('columns' =&gt; array('One', 'Two', 'Three'))), 'Form'));
</pre>
<p>Here I use my custom decorator, what it does is basically adding table/thead/th tags for the form. Titles for the columns can be assigned at &#8220;columns&#8221; parameter. You can download this decorator with all code for the article <a href="../?download=Table_Form">here</a>.</p>
<p><strong>3. Define rows.</strong></p>
<p>Each row of our form will be a subform of our main form.  Obviously we&#8217;ll have several rows in our table, so let&#8217;s code creation  of such a &#8220;row form&#8221; in a function in order to be able to call it  multiple times later:</p>
<pre class="brush: php; title: ; notranslate">
	public function addRow()
	{
		$row_form = new Zend_Form(array(
			'elements' =&gt; array(
				'one' =&gt; array(
					'type' =&gt; 'checkbox'
				),
				'two' =&gt; array(
					'type' =&gt; 'text'
				),
				'three' =&gt; array(
					'type' =&gt; 'text'
				),
			),
			'decorators' =&gt; array('FormElements', array('HtmlTag', array('tag'=&gt;'tr'))),
			'elementDecorators' =&gt; array('ViewHelper', array('HtmlTag', array('tag'=&gt;'td')))
		));

		$new_form_index = count($this-&gt;_subForms)+1;
		$row_form-&gt;setElementsBelongTo($new_form_index);
		$this-&gt;addSubform($row_form, $new_form_index);

		return $row_form;
	}
</pre>
<p>So here we:</p>
<ul>
<li>define elements which we need.</li>
<li> assign form decorators &#8211; it will wrap the entire row in &#8220;&lt;tr&gt;&lt;/tr&gt;&#8221; tags</li>
<li>assign elements decorators &#8211; it will wrap each single element in &#8220;&lt;td&gt;&lt;/td&gt;&#8221; tags</li>
<li>use setElementsBelongTo() in order to add unique prefix to the element name on each row of our form</li>
<li>finally, add this subform to the main form using $this-&gt;addSubform()</li>
</ul>
<p><strong>4. Add rows to the form.</strong></p>
<p>So now as we have defined our rows, let&#8217;s add some to our main form.  Let it be 3 rows for this form:</p>
<pre class="brush: php; title: ; notranslate">
$this-&gt;addRow();
$this-&gt;addRow();
$this-&gt;addRow();
</pre>
<p><strong>5. Almost done.</strong></p>
<p>Ok, this is it. Form shows nicely as a table. But what&#8217;s wrong with our &#8220;submit&#8221; button? The problem here that all content is wrapped into the table, including our submit button. To make our submit button display nicely we need to add tr/td around it as well. Also we need to make sure it shows under all other rows, we can do it by assigning some large number for &#8220;order&#8221;:</p>
<pre class="brush: php; title: ; notranslate">
$this-&gt;getElement('submit')-&gt;setDecorators(array('ViewHelper', array(array('td'=&gt;'HtmlTag'), array('tag'=&gt;'td', 'colspan'=&gt;3)), array(array('tr'=&gt;'HtmlTag'), array('tag'=&gt;'tr'))));
$this-&gt;getElement('submit')-&gt;setOrder(100);
</pre>
<p><strong>6. Enjoy</strong></p>
<p>The good thing is that after submit you will get a nicely structured array of all values like this:</p>
<pre class="brush: php; title: ; notranslate">
Array
(
    [1] =&gt; Array
        (
            [one] =&gt; 0
            [two] =&gt; row1 test
            [three] =&gt; row1 second test
        )

    [2] =&gt; Array
        (
            [one] =&gt; 1
            [two] =&gt; row2 test
            [three] =&gt; row2 second test
        )

    [3] =&gt; Array
        (
            [one] =&gt; 0
            [two] =&gt; row3 test
            [three] =&gt; row3 second test
        )

)
</pre>
<p>I used a very similar approach for alot of projects, hope this will be helpful for you also. Combining this idea with some javascript you can also create dynamic forms using Zend Form. Also, each row does not neccessary need to be the same, you can create different set of contorls on each row, just make sure you apply appropriate decorators. So hopefully this was a good starting point for you, which you can use to implement your ideas.</p>
<p>You can download entire code for this article <a href="http://paveldubinin.com/?download=Table_Form">here</a></p>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Real+table+based+form+with+Zend+Form+http%3A%2F%2Fpaveldubinin.com%2F2011%2F04%2Freal-table-based-form-with-zend-form%2F"><img class="nothumb" src="http://paveldubinin.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter6.png" alt="Post to Twitter" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://paveldubinin.com/2011/04/real-table-based-form-with-zend-form/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>7 quick tips on Zend Form</title>
		<link>http://paveldubinin.com/2011/04/7-quick-tips-on-zend-form/</link>
		<comments>http://paveldubinin.com/2011/04/7-quick-tips-on-zend-form/#comments</comments>
		<pubDate>Wed, 06 Apr 2011 07:39:11 +0000</pubDate>
		<dc:creator>Pavel Dubinin</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[Zend Form]]></category>

		<guid isPermaLink="false">http://paveldubinin.com/?p=107</guid>
		<description><![CDATA[Let me show you several quick tips on zend form, which are very simple, but seem to be hard to come across for many people﻿: 1. Custom HTML within the label Sometimes there&#8217;s a need to add some HTML directly into the label. For example, a quick link to &#8220;read more&#8221; or something. Normally you [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=7+quick+tips+on+Zend+Form+http%3A%2F%2Fpaveldubinin.com%2F2011%2F04%2F7-quick-tips-on-zend-form%2F"><img class="nothumb" src="http://paveldubinin.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter6.png" alt="Post to Twitter" /></a></p></div><p>Let me show you several quick tips on zend form, which are very simple, but seem to be hard to come across for many people﻿:<strong> </strong></p>
<h4><strong>1. Custom HTML within the label</strong></h4>
<p>Sometimes there&#8217;s a need to add some HTML directly into the label. For example, a quick link to &#8220;read more&#8221; or something. Normally you cannot do this, as Zend will apply escaping to the label. So if you try this:</p>
<pre class="brush: php; title: ; notranslate">
		$form = new Zend_Form();
		$element = $form-&gt;createElement('checkbox', 'username');
		$element-&gt;setLabel('I agree to the terms (&lt;a href=&quot;/terms&quot;&gt;Terms of Service&lt;/a&gt;): ');
		$form-&gt;addElement($element);
		echo $form;
</pre>
<p>It will output something like this:</p>
<blockquote><form enctype="application/x-www-form-urlencoded" method="post">
<dl class="zend_form">
<dt id="username-label"><label class="optional" for="username">I agree to the terms (&lt;a href=&#8221;#terms&#8221;&gt;Terms of Service&lt;/a&gt;):</label></dt>
<dd id="username-element">
<input name="username" type="hidden" value="0" />
<input id="username" name="username" type="checkbox" value="1" /></dd>
</dl>
</form>
</blockquote>
<p>To prevent Zend doing it, all you need to do is disabling escaping for the element like this:</p>
<pre class="brush: php; title: ; notranslate">
		$element-&gt;getDecorator('Label')-&gt;setOption('escape', false);
</pre>
<blockquote><form enctype="application/x-www-form-urlencoded" method="post">
<dl class="zend_form">
<dt id="username-label"><label class="optional" for="username">I agree to the terms (<a href="/terms">Terms of Service</a>):</label></dt>
<dd id="username-element">
<input name="username" type="hidden" value="0" />
<input id="username" name="username" type="checkbox" value="1" /></dd>
</dl>
</form>
</blockquote>
<h4><strong>2. Custom text or HTML anywhere on the form</strong></h4>
<p>Here&#8217;s a quick way to add any text or HTML into zend form:</p>
<pre class="brush: php; title: ; notranslate">
		$form-&gt;addElement('hidden', 'test', array(
			'description' =&gt; 'Hello world! &lt;a href=&quot;#&quot;&gt;Check it out&lt;/a&gt;',
			'ignore' =&gt; true,
			'decorators' =&gt; array(
				array('Description', array('escape'=&gt;false, 'tag'=&gt;'')),
			),
		));
</pre>
<p>This kind of hacky, but still works nicely and is much better than writting all in pure HTML.<br />
So here we create a fake hidden element, but tell Zend Framework that it does not really need to render it (have a look, we only have Description decorator here).</p>
<p>So only description itself is rendered (you can set it at &#8220;description&#8221; param). I&#8217;ve also added ignore=true, so that we don&#8217;t get that value each time calling $form-&gt;getValues(); as it will always be null anyway.</p>
<h4><strong>3. Placing label after element</strong></h4>
<p>By default all labels are placed before elements. In most cases this is very usable, but not for checkboxes<strong>. </strong>Usually you anticipate that there will be a checkbox and then some comment for it. The trick here is that &#8216;Label&#8217; decorator has option placement=&#8217;prepend&#8217;<strong> </strong>, which means that it will be rendered before anything else. To solve it, just set it to &#8220;append&#8221;:</p>
<pre class="brush: php; title: ; notranslate">
		$element-&gt;getDecorator('Label')-&gt;setOption('placement', 'append');
</pre>
<h4><strong>4. Inline radio buttons</strong></h4>
<p>If you try to add radio group, each radio button will be displayed vertically. This happens because by default zend adds &#8220;&lt;br/&gt;&#8221; at the end of each radiobutton. To prevent this, you can simply change that separator to emply string like this:</p>
<pre class="brush: php; title: ; notranslate">

$element-&gt;setSeparator('');
</pre>
<p>After applying this your radio buttons will be nicely displayed in one line.</p>
<h4><strong>5. Zend Form renders invalid HTML?</strong></h4>
<p>Ok, if you&#8217;re new to this, you might come to the situation where Zend generates HTML code which does not pass w3c validation, i.e. does not close tags etc. Don&#8217;t panic, this is somewhat expected! Zend Form renders elements based on your doctype settings. By default doctype is set to &#8220;HTML4_LOOSE&#8221;, according to that it is not required to close tags. But let us be up to date and use something more serious (i.e. XHTML strict). To do this you will need to add following code to your Bootstrap.php file:</p>
<pre class="brush: php; title: ; notranslate">
    protected function _initDoctype() {
        $this-&gt;bootstrap('view');
        $view = $this-&gt;getResource('view');
        $view-&gt;doctype('XHTML1_STRICT');
    }
</pre>
<p>This solves the problem and we have a nice and valid output now.</p>
<h4><strong>6. Global form errors (not related to any specific element)</strong></h4>
<p>That&#8217;s quite common task. For example, you want to create a login form. I&#8217;m not going to describe a complete login solution here, I will just use it as an example to demostrate global form errors.</p>
<p>Let us start like this:</p>
<pre class="brush: php; title: ; notranslate">
		$form = new Zend_Form();

		$form-&gt;setMethod('post');

		$form-&gt;addElement('text', 'username', array(
			'label' =&gt; 'Username: ',
			'required' =&gt; true
		));

		$form-&gt;addElement('password', 'password', array(
			'label' =&gt; 'Password: ',
			'required' =&gt; true
		));

		$form-&gt;addElement('submit', 'submit', array(
			'label' =&gt; 'Submit',
		));

		if ($this-&gt;getRequest()-&gt;isPost()) {
			$post = $this-&gt;getRequest()-&gt;getPost();

			if ($form-&gt;isValid($post)) {
				var_dump($form-&gt;getValues());
			}
		}

		echo $form;
</pre>
<p>This code will validate username and password fields separately and will display errors under them if any.<br />
But then you also need to check if this user/password combination actually exists at the database. And if it does not, error will need to be displayed. This is not username element&#8217;s error, not password&#8217;s, it&#8217;s more like form&#8217;s error. So let&#8217;s add it to the form itself:</p>
<pre class="brush: php; title: ; notranslate">
		if ($this-&gt;getRequest()-&gt;isPost()) {
			$post = $this-&gt;getRequest()-&gt;getPost();

			if ($form-&gt;isValid($post)) {
				if (check_login($post['username'], $post['password'])===false) {
					$form-&gt;setErrors(array('Invalid login'));
				} else {
					var_dump($form-&gt;getValues());
				}
			}
		}
</pre>
<p>check_login() is not a real function, it will just need to be some code that checks user details at the database. So, if user was not found we assign error to the form:</p>
<pre class="brush: php; title: ; notranslate">
$form-&gt;setErrors(array('Invalid login'));
</pre>
<p>By default these errors won&#8217;t be displayed, you&#8217;ll need to add a special decorator called FormErrors to do show them. Also set it&#8217;s parameter &#8220;onlyCustomFormErrors&#8221; to true, otherwise ALL errors (including errors from other elements) will be shown:</p>
<pre class="brush: php; title: ; notranslate">
$form-&gt;addDecorator('FormErrors', array('onlyCustomFormErrors'=&gt;true));
</pre>
<h4><strong>7. Decorators for multiple elements</strong></h4>
<p>If you want to change decorators for multiple elements, updating each single element can be a tedious task.<br />
Luckily Zend has functionality to change all decorators for the form elements at once using setElementDecorators() method:</p>
<pre class="brush: php; title: ; notranslate">

		$form = new Zend_Form();
		$form-&gt;setMethod('post');

		$form-&gt;addElement('text', 'message', array(
			'label' =&gt; 'Message: ',
			'required' =&gt; true
		));

		$form-&gt;addElement('submit', 'submit', array(
			'label' =&gt; 'Submit',
		));

		$form-&gt;addElement('reset', 'reset', array(
			'label' =&gt; 'Reset',
		));

		$form-&gt;setElementDecorators(array('Label', 'ViewHelper'));
		$form-&gt;setElementDecorators(array('ViewHelper'), array('submit', 'reset'));

		echo $form;
</pre>
<p>At the example above I have one input field and 2 buttons. Normally buttons do not need label before them, so they need to be styled differently. So the code says: &#8220;add default decorators to all elements&#8221;:</p>
<pre class="brush: php; title: ; notranslate">
$form-&gt;setElementDecorators(array('Label', 'ViewHelper'));
</pre>
<p>&#8220;but set different decorators for <strong>submit </strong>and <strong>reset </strong>elements&#8221;</p>
<pre class="brush: php; title: ; notranslate">
$form-&gt;setElementDecorators(array('ViewHelper'), array('submit', 'reset'));
</pre>
<p>Obviously, you can set any decorators and any elements you want. Here, I used these decorators just for example, normally you would want a bit different set.</p>
<p>Alirght, so that finishes my tips list for today.</p>
<p>What tricks  do you use yourself? Please share it at the comments section, I would definitely be interested to hear!</p>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=7+quick+tips+on+Zend+Form+http%3A%2F%2Fpaveldubinin.com%2F2011%2F04%2F7-quick-tips-on-zend-form%2F"><img class="nothumb" src="http://paveldubinin.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter6.png" alt="Post to Twitter" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://paveldubinin.com/2011/04/7-quick-tips-on-zend-form/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Zend Form &#8211; Radio buttons</title>
		<link>http://paveldubinin.com/2011/04/zend-form-radio-buttons/</link>
		<comments>http://paveldubinin.com/2011/04/zend-form-radio-buttons/#comments</comments>
		<pubDate>Tue, 05 Apr 2011 09:14:56 +0000</pubDate>
		<dc:creator>Pavel Dubinin</dc:creator>
				<category><![CDATA[Zend Framework]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[Radio button]]></category>
		<category><![CDATA[Zend Form]]></category>

		<guid isPermaLink="false">http://paveldubinin.com/?p=54</guid>
		<description><![CDATA[Zend Framework is very powerful and feature rich. But sometimes it can drive you nuts! While working on one of my projects I needed to build a form looking something like this: Looks pretty simple, but the trick is that I needed so that these were not 3 individual radio groups (Fruits, Veggies, Berries), but [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Zend+Form+%E2%80%93+Radio+buttons+http%3A%2F%2Fpaveldubinin.com%2F2011%2F04%2Fzend-form-radio-buttons%2F"><img class="nothumb" src="http://paveldubinin.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter6.png" alt="Post to Twitter" /></a></p></div><p>Zend Framework is very powerful and feature rich. But sometimes it can drive you nuts!<br />
While working on one of my projects I needed to build a form looking something like this:</p>
<p><a href="http://paveldubinin.com/wp-content/uploads/radiobuttons.png"><img class="alignnone wp-image-88" title="radiobuttons" src="http://paveldubinin.com/wp-content/uploads/radiobuttons.png" alt="" /></a></p>
<p>Looks pretty simple, but the trick is that I needed so that these were not 3 individual radio groups (Fruits, Veggies, Berries), but a single one. In other words, make it so that just one selection possible among all these radio buttons.</p>
<h1>Difficulties</h1>
<p>Zend Form allows us to create a group of radio buttons without a problem, so I started with something like this:</p>
<pre class="brush: php; title: ; notranslate">
		$form-&gt;addElement('radio', 'radio_group', array(
			'label' =&gt; 'Options:',
			'multioptions' =&gt; array(
				1 =&gt; 'Banana',
				2 =&gt; 'Apple',
				3 =&gt; 'Pear',
				4 =&gt; 'Cucumber',
				5 =&gt; 'Tomato',
				6 =&gt; 'Potato',
				7 =&gt; 'Blackberries',
				8 =&gt; 'Raspberries',
				9 =&gt; 'Strawberries',
			),
			'required' =&gt; true,
		));
</pre>
<p>Ok, now how do we place some text(or whatever) in between so that to describe  groups? My next idea was to split that one big radio group into 3 smaller ones:</p>
<pre class="brush: php; title: ; notranslate">
		$form-&gt;addElement('radio', 'fruits', array(
			'label' =&gt; 'Fruits:',
			'multioptions' =&gt; array(
				1 =&gt; 'Banana',
				2 =&gt; 'Apple',
				3 =&gt; 'Pear',
			),
		));

		$form-&gt;addElement('radio', 'veggies', array(
			'label' =&gt; 'Veggies:',
			'multioptions' =&gt; array(
				4 =&gt; 'Cucumber',
				5 =&gt; 'Tomato',
				6 =&gt; 'Potato',
			),
		));

		$form-&gt;addElement('radio', 'berries', array(
			'label' =&gt; 'Berries:',
			'multioptions' =&gt; array(
				7 =&gt; 'Blackberries',
				8 =&gt; 'Raspberries',
				9 =&gt; 'Strawberries',
			),
			'required' =&gt; true,
		));
</pre>
<p>Ok, that looks better, but now we have 3 separate groups, rather than one and you can have 3 selections and that&#8217;s not what we wanted to archive.</p>
<p>The problem here is that Zend Form elements are generated in such a way so that it puts name of the element into name of the HTML input control. So we have something like this (simplified):</p>
<pre class="brush: xml; title: ; notranslate">

&lt;input name=&quot;fruits&quot; type=&quot;radio&quot; value=&quot;1&quot; /&gt; Banana
&lt;input name=&quot;fruits&quot; type=&quot;radio&quot; value=&quot;2&quot; /&gt; Apple
&lt;input name=&quot;fruits&quot; type=&quot;radio&quot; value=&quot;3&quot; /&gt; Pear

&lt;input name=&quot;veggies&quot; type=&quot;radio&quot; value=&quot;4&quot; /&gt; Cucumber
&lt;input name=&quot;veggies&quot; type=&quot;radio&quot; value=&quot;5&quot; /&gt; Tomato
&lt;input name=&quot;veggies&quot; type=&quot;radio&quot; value=&quot;6&quot; /&gt; Potato

&lt;input name=&quot;berries&quot; type=&quot;radio&quot; value=&quot;7&quot; /&gt; Blackberries
&lt;input name=&quot;berries&quot; type=&quot;radio&quot; value=&quot;8&quot; /&gt; Raspberries
&lt;input name=&quot;berries&quot; type=&quot;radio&quot; value=&quot;9&quot; /&gt; Strawberries
</pre>
<p>From the other hand, in order to make a radio group with HTML, you need to have the same name for all radio buttons.</p>
<p>With Zend Form it is impossible to just rename all 3 elements to have one name, as each element is required to have an unique name. Specifying name as an additional attibute for the control works to some extent, but it breaks other things like validation, repopulating radio group and others.</p>
<h1>Solution</h1>
<p>Ok, so I could give up and make everything with ViewScript writting pure HTML, but thinking about it I realized that this kind of task is quite frequent (especially when dealing with some kind of quiz or stuff like that). And I did not want to finish up having all my forms being decoupled from Zend Forms in favor of pure HTML. So I decided to make a custom form element to solve that, hopefully it will be useful for you also.</p>
<p>Let me first describe how we can use it, and later I will describe details of implementation if you&#8217;re interested:</p>
<ul>
<li><strong>First of all, we just create our &#8220;core&#8221; radio group containing all possible options:</strong></li>
</ul>
<pre class="brush: php; title: ; notranslate">
		$form-&gt;addElement('radio', 'radio_group', array(
			'label' =&gt; 'Options:',
			'multioptions' =&gt; array(
				1 =&gt; 'Banana',
				2 =&gt; 'Apple',
				3 =&gt; 'Pear',
				4 =&gt; 'Cucumber',
				5 =&gt; 'Tomato',
				6 =&gt; 'Potato',
				7 =&gt; 'Blackberries',
				8 =&gt; 'Raspberries',
				9 =&gt; 'Strawberries',
			),
			'required' =&gt; true,
		));
</pre>
<p>This radio group will be used as a starting point for the other smaller groups. Why is it usable to have it, is that we can use validation configuration in just this one place, also by placing this element in different places we can control where exactly we want validation errors to be shown. And, when receiving value after form was validated, we don&#8217;t want to go and check at multiple locations which radiobuttom has which value, we can just check our &#8220;core radio button&#8221; for it.</p>
<ul>
<li><strong>We don&#8217;t want this element to output any extra markup, so lets leave only &#8220;Errors&#8221; decorator for it:</strong></li>
</ul>
<pre class="brush: php; title: ; notranslate">
		$radio_parent = $form-&gt;getElement('radio_group');
		$radio_parent-&gt;setDecorators(array('Errors'));
</pre>
<ul>
<li><strong>We add 3 more radio groups for each set (Fruits, Veggies, Berries).</strong></li>
</ul>
<p>This time we&#8217;ll use extended version of Zend_Form_Element_Radio class which I&#8217;ve created, called &#8220;standaloneRadio&#8221;, like this:</p>
<pre class="brush: php; title: ; notranslate">
		$form-&gt;addPrefixPath('ZFExt_Form_Element', 'ZFExt/Form/Element/', 'Element');

		$form-&gt;addElement('standaloneRadio', 'fruits', array(
			'bind' =&gt; array($radio_parent, array(1,2,3)),
			'label' =&gt; 'Fruits',
		));
</pre>
<p>It has an extra &#8220;bind&#8221; parameter which is an array:<br />
- for the first item in this array you must use instance our &#8220;core radio group&#8221; element, created at step 1.<br />
- for the second you specify which options from it you want to use.</p>
<p>So at the example above we refer to our core element ($radio_parent) and use options with IDs 1,2 and 3 from it, i.e. Banana, Apple, Pear. We&#8217;ll need to do the same thing for our &#8220;veggies&#8221; and &#8220;berries&#8221; elements, refering to corresponding options:</p>
<pre class="brush: php; title: ; notranslate">
		$form-&gt;addElement('standaloneRadio', 'veggies', array(
			'bind' =&gt; array($radio_parent, array(4,5,6)),
			'label' =&gt; 'Veggies',
		));

		$form-&gt;addElement('standaloneRadio', 'berries', array(
			'bind' =&gt; array($radio_parent, array(7,8,9)),
			'label' =&gt; 'Berries',
		));
</pre>
<p>Basically that&#8217;s it! Now, when you validate the form and get the values, you will only need to refer to out &#8216;radio_group&#8217; element and not individual elements:</p>
<pre class="brush: php; title: ; notranslate">
		$form-&gt;radio_group-&gt;getValue();
</pre>
<p>Same thing applies when you want to populate form with default values &#8211; just assign it to the &#8220;radio_group&#8221; element and it will work as expected.</p>
<h1>Implementation Details</h1>
<p>So now let&#8217;s have a look at that custom class I&#8217;ve created:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php
class ZFExt_Form_Element_StandaloneRadio extends Zend_Form_Element_Radio
{
	//Parent element on which this radiobutton depends
	protected $_bindParent = null;

	//All validation is done at parent ($_bindParent), so we disable it for individual radiobuttons
	protected $_registerInArrayValidator = false;

	//This element depends on parent ($_bindParent), so we don't need its value directly
	protected $_ignore = true;

	public function setOptions(array $options)
	{
		//Accept additional bind parameter, which is an array:
		//0 - link to the parent radiobutton element (must instance of Zend_Form_Element_Radio)
		//1 - array of option ids which current element needs to take from its parent
		if (isset($options['bind'][0]) &amp;&amp; $options['bind'][0] instanceof Zend_Form_Element_Radio) {
			$parent = $options['bind'][0];
			$binding_multioptions = isset($options['bind'][1])?$options['bind'][1]:null;

			if (!empty($binding_multioptions)) {
				if (!is_array($binding_multioptions)) {
					$binding_multioptions = array($binding_multioptions);
				}

				//Attach selected parent multioptions to the current element
				$parent_multioptions = $parent-&gt;getMultiOptions();
				$multioptions = array();
				foreach ($binding_multioptions as $current_option_id) {
					if (isset($parent_multioptions[$current_option_id])) {
						$multioptions[$current_option_id] = $parent_multioptions[$current_option_id];
					}
				}
				$this-&gt;addMultiOptions($multioptions);
			}

			$this-&gt;_bindParent = $parent;

			//We've stored all neccessary info, no need in this parameter anymore
			unset($options['bind']);
		}

		//Call original setOptions from Zend_Form_Element_Radio
		parent::setOptions($options);

		return $this;
	}

	public function render(Zend_View_Interface $view = null)
	{
		//Here we replace element name with parent name, but just for rendering
		$current_name = $this-&gt;getName();
		$this-&gt;setName($this-&gt;_bindParent-&gt;getName());

		$output = parent::render($view);

		//Put the real name back
		$this-&gt;setName($current_name);

		return $output;
	}

	public function getValue()
	{
		//Element is dependent on parent
		return $this-&gt;_bindParent-&gt;getValue();
	}

	public function setValue($value)
	{
		//Element is dependent on parent
		return $this-&gt;_bindParent-&gt;setValue($value);
	}

	public function isValid($value, $context = null)
	{
		//Assign same value to all members of the group
		$value = $this-&gt;_bindParent-&gt;getValue();
		$context[$this-&gt;_name] = $value;

		return parent::isValid($value, $context);
	}
}
</pre>
<p>This class is inherited from Zend_Form_Element_Radio as we want to take all features from it and just extend a little.<br />
There&#8217;s not too much code and I commented it alot, so hopefully it will be easy to understand. But still, I will explain few points:</p>
<p>First of all, it overrides setOptions() function so that to read that extra &#8220;bind&#8221; parameter for our singular radio buttons.</p>
<p>It also overrides render() function. There I simply replace name of the individual radio group with the name of the &#8220;core element&#8221;. That way we ensure that all radio buttons in group will have the same name. Zend might need to use element name in many different places, so I&#8217;ve decided to put the original name back right after rendering.</p>
<p>Few more overrides for getValue() and setValue() functions. As you can see, they are now configured to get/set values directly from the core element. That way we ensure values are repopulated correctly on the form.</p>
<p>Last, and very importaint override is for isValid(). When data comes from HTML form, it only has value for &#8220;core element&#8221;. During validation we assign that same value for all linked radio buttons.</p>
<p>Alright, so that&#8217;s it! I hope that was useful. You can download entire code for this post <a href="http://paveldubinin.com/?download=Standalone-Radiobutton">here</a>.</p>
<p>And if you guys have any questions or comments I&#8217;d be glad to hear <img src='http://paveldubinin.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<div class="tweetthis" style="text-align:left;"><p> <a target="_blank" rel="nofollow" class="tt" href="http://twitter.com/intent/tweet?text=Zend+Form+%E2%80%93+Radio+buttons+http%3A%2F%2Fpaveldubinin.com%2F2011%2F04%2Fzend-form-radio-buttons%2F"><img class="nothumb" src="http://paveldubinin.com/wp-content/plugins/tweet-this/icons/en/twitter/tt-twitter6.png" alt="Post to Twitter" /></a></p></div>]]></content:encoded>
			<wfw:commentRss>http://paveldubinin.com/2011/04/zend-form-radio-buttons/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
