Thoai Nguyen

Nice guy, Bookworm, Hacker, Bootstrapper

Page 2


Pre-filling a silex form

Let’s say you want to populate another form with some data (on a GET request). This is a single-page Silex app and not a full Symfony2 stack with object based forms. We are talking about a form that uses simple array as its data model here.

The form is generated with the following piece of code:

$app->register(new \Silex\Provider\FormServiceProvider());
$app->register(new Silex\Provider\ValidatorServiceProvider());
$app->register(new \Silex\Provider\SessionServiceProvider());
$app->register(
    new Silex\Provider\TranslationServiceProvider(),
    array(
        'translator.domains' => array(),
    )
);

$form = $app['form.factory']->createBuilder('form')
    ->add('name','text',['label'=>'Full name'])
    // Add other forms fields here.
    ->getForm();

Assuming the pre-fill data is stored in the key-value array $data. My first attempt to fill the form was:

$form->submit($data);

Continue reading →