<?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>devexp &#187; forms</title>
	<atom:link href="http://www.devexp.eu/tag/forms/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.devexp.eu</link>
	<description>DEVelopment EXPerience, shared with the world!</description>
	<lastBuildDate>Fri, 28 Oct 2011 12:07:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Basic forms with Symfony</title>
		<link>http://www.devexp.eu/2009/04/30/basic-forms-with-symfony/</link>
		<comments>http://www.devexp.eu/2009/04/30/basic-forms-with-symfony/#comments</comments>
		<pubDate>Thu, 30 Apr 2009 11:12:57 +0000</pubDate>
		<dc:creator>van Rumste Kenneth</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[Symfony]]></category>
		<category><![CDATA[forms]]></category>
		<category><![CDATA[module]]></category>

		<guid isPermaLink="false">http://www.devexp.eu/?p=726</guid>
		<description><![CDATA[Our goal is to create a little crud that contains 4 fields id (auto increment integer) name (varchar 50) value (double) type (0 or 1) What I will explain is how to change your fields in the form after creation &#8230; <a href="http://www.devexp.eu/2009/04/30/basic-forms-with-symfony/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p><img class="alignright size-full wp-image-727" title="Symfony" src="http://www.devexp.eu/wp-content/uploads/2009/04/symfony.jpg" alt="Symfony" width="127" height="35" />Our goal is to create a little crud that contains 4 fields</p>
<p>id (auto increment integer)<br />
name (varchar 50)<br />
value (double)<br />
type (0 or 1)</p>
<p>What I will explain is how to change your fields in the form after creation with generation of a module. So we start with a clean module that contains 1 action class and 3 templates (new, edit and index) and 1 partial (form) that is included on the new and edit template.<br />
We created this module not by hand but with the very easy command: symfony propel:generate-module.</p>
<p><span id="more-726"></span>So what we want to do is quite easy, but if you are not used to working with forms, this is already a big struggle. So go to the action and you will see that in the executeNew method a form is initialized with an instance of a class (in our case DiscountForm).</p>
<pre class="brush: php; title: ; notranslate">
public function executeNew(sfWebRequest $request)
{
$this-&gt;form = new DiscountForm();
}
</pre>
<p>If you now go to the page in your browser you will see that the form is build up for you in a very basic way, containing no layout and just the 4 labels, the input fields and a submit button. If you want the user to see error messages when he has forgotten to fill in a required field or if you want to add a field to the form you can do that in the DiscountForm by adding a function configure().</p>
<pre class="brush: php; title: ; notranslate">
class DiscountForm extends BaseDiscountForm  {
public function configure() {
//insert extra data
}
}
</pre>
<p>Now, as we don&#8217;t want the user to select 0 or 1 when choosing his type, we let him use a dropdown with understandable content to choose from. 0 is discount and 1 is up count. The purpose of the type is that the number inserted in the value field will be added or subtracted.</p>
<p>To do this we create a public $type array containing the discount and up count in the Discount class in the model. We do this in the model class because, in this way, we can re-use this in other methods later as we might need this in the templates or other classes.</p>
<pre class="brush: php; title: ; notranslate">
static public $types = array(
'0' =&gt; 'Discount',
'1' =&gt; 'Up count',
);
</pre>
<p>And in the configure method we put this:</p>
<pre class="brush: php; title: ; notranslate">
$this-&gt;widgetSchema['type'] = new sfWidgetFormChoice(array(
'choices'  =&gt; Discount::$types,
'expanded' =&gt; true,
));
</pre>
<p>If you reload your view on this point you will see that the type field has changed to a radio button field, which is easier to read.</p>
<p>You can style the form by adding some attributes to your fields:</p>
<pre class="brush: php; title: ; notranslate">
&lt;?php echo $form['discount']-&gt;render(array('class' =&gt; 'page-input', 'size' =&gt; 5));  ?&gt;
</pre>
<p>Page-input is the class specified in the css and giving the field a proper layout and size is the width of the field, in this case 5.<br />
All other attributes your element can accept can be inserted like this.</p>
<p>That&#8217;s actually it, if you followed these easy steps, you created your own basic crud. How nice it all looks will all depend on the css you use. For a few templates on forms you can take a look at <a href="http://www.smashingmagazine.com/2006/11/11/css-based-forms-modern-solutions/" target="_blank">this post </a></p>
<div class="shr-publisher-726"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.devexp.eu/2009/04/30/basic-forms-with-symfony/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

