Basic forms with Symfony

SymfonyOur 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 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.
We created this module not by hand but with the very easy command: symfony propel:generate-module.

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).

public function executeNew(sfWebRequest $request)
$this->form = new DiscountForm();

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().

class DiscountForm extends BaseDiscountForm  {
public function configure() {
//insert extra data

Now, as we don’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.

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.

static public $types = array(
'0' => 'Discount',
'1' => 'Up count',

And in the configure method we put this:

$this->widgetSchema['type'] = new sfWidgetFormChoice(array(
'choices'  => Discount::$types,
'expanded' => true,

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.

You can style the form by adding some attributes to your fields:

<?php echo $form['discount']->render(array('class' => 'page-input', 'size' => 5));  ?>

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.
All other attributes your element can accept can be inserted like this.

That’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 this post

By the way, if you found a typo, please fork and edit this post. Thank you so much! This post is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.


Fork me on GitHub