Using Symfony’s sfForm Standalone

No Gravatar

Forms are something a web developer makes and maintains nearly every day. Despite their common occurrence they’re easy to do badly. Luckily they are something of a solved problem. There are many tools out there that help the PHP developer create forms including PEAR‘s HTML_QuickForm2, Zend Framework‘s Zend_Form, eZ Components‘s ezcInputForm, CodeIgniter‘s Form Helper, and many others. Having drunk the Symfony Kool-Aid and being pretty impressed with it I thought I would see if I could use their sfForm class in a non Symfony project.

How I Used sfForm in a Non Symfony Project

You’ll need the full Symfony source somewhere on your system. Download Symfony if you haven’t already. The current version at the time of this writing is 1.4.1. I extracted the tarball to /var/www/lib which made the directory /var/www/lib/symfony-1.4.1. Keep the path to your Symfony source in mind.

I’ll assume you have a PHP file exposed via your web server. Let’s make the sfForm and related classes avaliable in your PHP file by adding the following code:

<?php
$symfony_path = '/var/www/lib/symfony-1.4.1/lib'; // Change this to match your configuration.
require_once $symfony_path . '/autoload/sfCoreAutoload.class.php';
sfCoreAutoload::register();

That’s really all it takes, you can now use any Symfony class outside of a typical Symfony project. I’ve separated the above code into a separate bootstrap file so I can use Symfony classes by adding a single require_once statement (to said bootstrap file).

Here’s an example sfForm class:

<?php
class ExampleForm extends sfForm
{
  public function configure() 
  {
    $this->setWidgets(array(
      'example_text' => new sfWidgetFormInput()
    ));
 
    $this->setValidators(array(
      'example_text' => new sfValidatorString(array('max_length' => 255, 'required' => true))
    ));
 
    $this->widgetSchema->setLabels(array(
      'example_text' => 'Example Text'
    ));
 
    // I had to specify this, there wasn't any formatting by default.
    $this->widgetSchema->setFormFormatterName('table');
 
    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
 
    // Don't use $this->widgetSchema->setNameFormat()
    // unless you want to do some special parsing of $_POST data.
    // It's easier just to prefix your field names with something like
    // example_.
  }
}

Here’s a quick and dirty way to use the above form class:

<?php
$form = new ExampleForm(array(
  'example_text' => 'Default Value'
));
 
if(!empty($_POST))
{
  $form->bind($_POST);
 
  if($form->isValid())
  {
    var_dump($form->getValues());    
    exit;
  }
}
 
?>
<h1>Example Form</h1>
<form action="test.php" method="post">
  <table>
    <?php echo $form ?>
    <tr>
      <td>&nbsp;</td>
      <td>
        <input type="submit" value="Submit" />
      </td>
    </tr>
  </table>
</form>

There you have it, a working form to help you get started. There are a couple resources worth checking if you decide to use this approach: the Symfony Form Book and of course the API.

One Response to “Using Symfony’s sfForm Standalone”

  1. jmsNo Gravatar says:

    hi,

    thanks for this post !
    Do you think it is possible to use doctrine+sfForm in a standalone way ?

Leave a Reply