12.08.07

7 Days of Symfony 1.1 - Forms, Widgets and Validators (Day1)

Posted by ryan in symfony


Note: Many things here may be out of date. This article is up to date only through early 2008 (which was a while ago).

With the completion of the new Symfony 1.1 framework, there are many changes in store for the average Symfony developer. Some less visible changes in the framework include a refactoring of the core classes to increase modularity. This will cause several little changes, like, for example, the new location of the flash object as part of the user object:

// In action
$this->getUser()->getFlash('flash_name');

But the largest change, by far, is in the handling of forms, validation, and the helper system. During the next seven days, we'll be covering the new approach to Symfony forms and validation. The new solution is a MAJOR improvement over the old system- it will allow for faster development and greater control. Gone are the days of mindless form generation. The new system isn't perfect, but it's definitely worth your time.

As a side note, I mentioned that the new helper system is one of the major changes to the framework. Better said, Symfony 1.1 doesn't actually have a helper system. The helpers are either replaced by other parts of Symfony, or purposely abandoned as a core feature of the framework. See Fabien's explanation of how the helpers will be handled in the new Symfony: http://trac.symfony-project.com/wiki/Symfony11Helpers

We all know how we used to create forms – manually setting up our table or table-less formatting, inserting our labels, and using helpers for each piece of our form. This all took place inside our template, and resulted in a pretty heavy template for large forms. How will our new templates look? It'll look a little something like this (for a tabled form):

// In template
<form action="<?php echo url_for('@my_action'); ?>" method="post">
  <table class="form">
    <?php echo $form; ?>
    <tr>
      <td><input type="submit" value="submit me" /></td>
    </tr>
  </table>
</form>

That's it! Other than the form tags and submit tags, EVERYTHING is printed out via the echo $form line. By everything, I mean layout (in this case, and tags), labels, and the elements themselves. Where was the $form variable setup? Symfony sets it up for you based on your schema! Sure, we'll want to make our own modifications, but the new system basically lets you handle your forms – including validation, it just a few lines of code.

Come back tomorrow, we'll create a first forms, from almost no code.

Thanks for the shares!
  • StumbleUpon
  • Sphinn
  • del.icio.us
  • Facebook
  • TwitThis
  • Google
  • Reddit
  • Digg
  • MisterWong
Posted by form on 2011-08-26
Where are the forms you were going to create (tomorrow?)?