<?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>designing4u.de &#187; Symfony</title>
	<atom:link href="http://www.designing4u.de/tag/symfony/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.designing4u.de</link>
	<description>Yet Another Coding Blog</description>
	<lastBuildDate>Fri, 29 Jul 2011 08:11:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Symfony &#8211; no error message on empty values in not required fields</title>
		<link>http://www.designing4u.de/2008/07/symfony-no-error-message-on-empty-values-in-not-required-fields/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=symfony-no-error-message-on-empty-values-in-not-required-fields</link>
		<comments>http://www.designing4u.de/2008/07/symfony-no-error-message-on-empty-values-in-not-required-fields/#comments</comments>
		<pubDate>Sat, 19 Jul 2008 14:48:08 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[OOP PHP]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[emtpy values]]></category>
		<category><![CDATA[no error message]]></category>
		<category><![CDATA[not required]]></category>
		<category><![CDATA[not required field]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=25</guid>
		<description><![CDATA[It's been a long time since I wrote my last post, but I kept myself busy with work. I'm just working with another web 2.0 project (yes, there will be another web 2.0 project , which was written using symfony framework. I need to admit that my previous experience with different frameworks was pretty good, [...]]]></description>
			<content:encoded><![CDATA[<p>It's been a long time since I wrote my last post, but I kept myself busy with work. I'm just working with another web 2.0 project (yes, there will be another web 2.0 project <img src='http://www.designing4u.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> , which was written using symfony framework. I need to admit that my previous experience with different frameworks was pretty good, but symfony is different and I'm really excited about it. In a way it is similar to CakePHP using MVC patter but it has features, which I couldn't find in Cake. I mean symfony has its pros and cons but I find it really easy to build database models and modules using CLI and ORM. Propel is probably not the best abstraction layer, but I was reading on Doctrine site (which I personally find better than Propel) that it will be included in the new 1.2 release. I don't want to write a lot about symfony here, but rather solve a validation problem I encountered during development.<br />
<span id="more-25"></span><br />
Symfony has a really powerful configuration system, in which you can also define your validation rules. My problem was as follows, I have 3 different access control levels and for one of them an input field is required and for the rest not. Let's describe it with an example of a company.</p>
<p>You have managers, executives and simple workers. To validate a manager we generated codes which we later distributed between them. Executives and simple workers didn't have to use a validation code. Therefore a simple input field of type text was provided only if a person registering on our site chose the position manager. Therefore I couldn't make this field as required because it was only required for a certain group of people registering on our site. </p>
<p>The validation rule was simple. I compared the code provided during registration with the one saved in the database and returned true or false according to the case. The problem I encountered was in the case that if a person chose manager and did not provide any input (an empty field), the error message was not displayed at all. I mean you could probably solve this problem using JavaScript but server side validation is always important. Researching the core code of symfony I found a place where this case is not considered an error. I could possibly change the core, but I didn't want to do that only for one case. I mean, if it would be a problem with many fields I would probably consider it necessary, but I've found a solution for that problem without changing the core code. Here is how I accomplished it.</p>
<p>I assume that you are familiar with symfony framework, therefore I wont explain the whole process from the beginning. In my /apps/myproject/lib folder I created a class extending sfValidation class, where I placed the database check. Unfortunately, defining additional string validation rules ($string == null etc.) in that class did not solve my problem that an error did not appear when the field was empty.</p>
<p>I did some more reading and I've found a really interesting solution. In your action.class.php file besides executeXXX() methods you can define additional validateXXX() methods, which are executed before executeXXX() methods. I placed a simple string validation rule in this method (with min value of 1) and it works like a charm right now. Below you can see my code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> validateIndex<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">//manager code validation</span>
  <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequestParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'position'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'manager'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$managerCode</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequestParameter</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'manager_code'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$val</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> sfStringValidator<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$val</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">initialize</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">'min'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$val</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">execute</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$managerCode</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRequest</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setError</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'managerCode'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getContext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getI18N</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-&gt;</span>__<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'You have to fill out the code'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'user'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>As I said before this approach might get pretty annoying if you are dealing with multiple fields but for me it was just this one. I hope this solution will help you during your development. As usually I'm waiting for your feedback. Cheers!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/07/symfony-no-error-message-on-empty-values-in-not-required-fields/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

