sfForms11Plugin: use symfony 1.1 form/validation framework in symfony 1.0 or non-symfony project
By Romain Dorgueil on Monday, January 7 2008, 23:32 - plugin
The new symfony 1.1 form/validation framework is 100% symfony-independant, and though is not limited to the (very) awaited 1.1 stable release. To use it in your own symfony 1.0 projects, you can use sfForms11Plugin. It's content is very thin: it only sets up externals to symfony 1.1 form/widget/validator libraries.
To use it, simply add the following external to your project:
sfForms11Plugin http://svn.symfony-project.com/plugins/sfForms11Plugin
Or check it out from your project base directory:
svn co http://svn.symfony-project.com/plugins/sfForms11Plugin plugins/sfForms11Plugin
Let's try it...
Now you'll be able to define sfForm sub-classes to materialize forms:
class HelloWorldForm extends sfForm
{
public function configure()
{
}
public function setup()
{
$this->setWidgetSchema(new sfWidgetFormSchema(array(
'name' => new sfWidgetFormInput(),
)));
$this->setValidatorSchema(new sfValidatorSchema(array(
'name' => new sfValidatorString(array('required'=>true, 'min_length'=>1, 'max_length'=>50)),
)));
$this->widgetSchema->setNameFormat('hello[%s]');
$this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);
parent::setup();
}
}This class define the model of your HelloWorldForm, and you can now use it in your actions:
class testActions extends sfActions
{
function executeHelloWorld()
{
$this->form = new HelloWorldForm();
if ($this->getRequest()->getMethod() == sfRequest::POST)
{
if (null !== ($hello = $this->getRequestParameter('hello', null)))
{
$this->form->bind($hello);
if ($this->form->isValid())
{
$this->redirect('@hello?name='.$this->form->getValue('name'));
}
}
}
}
}At this point, the only little detail still missing is the view, containing actual form display:
<form method="POST">
<table>
<?php echo $form; ?>
<tr>
<td colspan="2" align="right">
<input type="submit" value="Greetings, Mr Computer!" />
</td>
</tr>
</table>
</form>That's it! You now have a simple form, self-validating, protected against CSRF attacks and redirecting to some place if values entered matched our sfValidatorSchema.






Comments
would be nice to see an extension for the admn generator so the form of the 1.0 generator are also using the new framework..
Admin generator will of course be rewritten for symfony 1.1, but it would be too much work to create a different one for symfony 1.0 using 1.1 forms knowing that it would be born-to-be-burnt code...
I look forward to symfony 1.1 admin generator too, but admin generation even if it is a very cool symfony feature, is imho not at all one of the most important.this is good