Commit 5b875899 authored by Fabien Potencier's avatar Fabien Potencier

feature #1309 add a namedForm() method in FormTrait (quazardous)

This PR was squashed before being merged into the 2.0.x-dev branch (closes #1309).

Discussion
----------

add a namedForm() method in FormTrait

I do prefer using the namedForm builder.

Just add the `$this['form.factory']->createNamedBuilder(...)` in `Silex\Application\FormTrait`:

```php
    /**
     * Creates and returns a named form builder instance.
     *
     * @param string                   $name
     * @param mixed                    $data    The initial data for the form
     * @param array                    $options Options for the form
     * @param string|FormTypeInterface $type    Type of the form
     *
     * @return \Symfony\Component\Form\FormBuilder
     */
    public function namedForm($name, $data = null, array $options = array(), $type = null)
    {
        if (null === $type) {
            // BC with Symfony < 2.8
            $type = class_exists('Symfony\Component\Form\Extension\Core\Type\RangeType') ? 'Symfony\Component\Form\Extension\Core\Type\FormType' : 'form';
        }

        return $this['form.factory']->createNamedBuilder($name, $type, $data, $options);
    }
```

Commits
-------

c8e61fc2 add a namedForm() method in FormTrait
parents 013d25ca c8e61fc2
......@@ -17,6 +17,7 @@ use Symfony\Component\Form\FormBuilder;
* Form trait.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author David Berlioz <berliozdavid@gmail.com>
*/
trait FormTrait
{
......@@ -37,4 +38,25 @@ trait FormTrait
return $this['form.factory']->createBuilder($type, $data, $options);
}
/**
* Creates and returns a named form builder instance.
*
* @param string $name
* @param mixed $data The initial data for the form
* @param array $options Options for the form
* @param string|FormTypeInterface $type Type of the form
*
* @return \Symfony\Component\Form\FormBuilder
*/
public function namedForm($name, $data = null, array $options = array(), $type = null)
{
if (null === $type) {
// BC with Symfony < 2.8
$type = class_exists('Symfony\Component\Form\Extension\Core\Type\RangeType') ? 'Symfony\Component\Form\Extension\Core\Type\FormType' : 'form';
}
return $this['form.factory']->createNamedBuilder($name, $type, $data, $options);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment