Commit 31bbe3e5 authored by Fabien Potencier's avatar Fabien Potencier

enhanced documentation for forms

parent ce382188
...@@ -52,13 +52,20 @@ Usage ...@@ -52,13 +52,20 @@ Usage
The FormServiceProvider provides a ``form.factory`` service. Here is a usage The FormServiceProvider provides a ``form.factory`` service. Here is a usage
example:: example::
$app->get('/hello/{name}', function ($name) use ($app) { $app->match('/form', function (Request $request) use ($app) {
return "Hello $name!"; // some default data for when the form is displayed the first time
})->bind('hello'); $data = array(
'name' => 'Your name',
$app->match('/', function (Request $request) use ($app) { 'email' => 'Your email',
$form = $app['form.factory']->createBuilder('form') );
->add('name', 'text')
$form = $app['form.factory']->createBuilder('form', $data)
->add('name')
->add('email')
->add('gender', 'choice', array(
'choices' => array(1 => 'male', 2 => 'female'),
'expanded' => true,
))
->getForm(); ->getForm();
if ('POST' == $request->getMethod()) { if ('POST' == $request->getMethod()) {
...@@ -69,14 +76,16 @@ example:: ...@@ -69,14 +76,16 @@ example::
// do something with the data // do something with the data
return $app->redirect('/hello/{name}'); // redirect somewhere
return $app->redirect('...');
} }
} }
// display the form
return $app['twig']->render('index.twig', array('form' => $form->createView())); return $app['twig']->render('index.twig', array('form' => $form->createView()));
}); });
Put this in your template file named ``views/index.twig``: And here is the ``index.twig`` form template:
.. code-block:: jinja .. code-block:: jinja
...@@ -100,6 +109,14 @@ form by adding constraints on the fields:: ...@@ -100,6 +109,14 @@ form by adding constraints on the fields::
->add('name', 'text', array( ->add('name', 'text', array(
'constraints' => array(new Assert\NotBlank(), new Assert\MinLength(5)) 'constraints' => array(new Assert\NotBlank(), new Assert\MinLength(5))
)) ))
->add('email', 'text', array(
'constraints' => new Assert\Email()
))
->add('gender', 'choice', array(
'choices' => array(1 => 'male', 2 => 'female'),
'expanded' => true,
'constraints' => new Assert\Choice(array(1, 2)),
))
->getForm(); ->getForm();
For more information, consult the `Symfony2 Forms documentation For more information, consult the `Symfony2 Forms documentation
......
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