Commit ce382188 authored by Fabien Potencier's avatar Fabien Potencier

fixed documentation for forms

parent 82fa4ef8
...@@ -30,22 +30,9 @@ Registering ...@@ -30,22 +30,9 @@ Registering
.. code-block:: php .. code-block:: php
use Silex\Provider\SymfonyBridgesServiceProvider;
use Silex\Provider\TranslationServiceProvider;
use Silex\Provider\FormServiceProvider; use Silex\Provider\FormServiceProvider;
$app->register(new SymfonyBridgesServiceProvider(), array( $app->register(new FormServiceProvider());
'symfony_bridges.class_path' => __DIR__.'/vendor/symfony/src'
));
$app->register(new TranslationServiceProvider(), array(
'translation.class_path' => __DIR__.'/vendor/symfony/src',
'translator.messages' => array()
));
$app->register(new FormServiceProvider(), array(
'form.class_path' => __DIR__.'/vendor/symfony/src'
));
.. note:: .. note::
...@@ -69,51 +56,51 @@ example:: ...@@ -69,51 +56,51 @@ example::
return "Hello $name!"; return "Hello $name!";
})->bind('hello'); })->bind('hello');
$app->match('/', function () use ($app) { $app->match('/', function (Request $request) use ($app) {
$validation_constraint = new Collection(array(
'name' => new NotBlank(),
));
$form = $app['form.factory']->createBuilder('form') $form = $app['form.factory']->createBuilder('form')
->add('name', 'text') ->add('name', 'text')
->getForm(); ->getForm();
$request = $app['request'];
if ('POST' == $request->getMethod()) { if ('POST' == $request->getMethod()) {
$form->bindRequest($request); $form->bindRequest($request);
if ($form->isValid()) { if ($form->isValid()) {
$data = $form->getData(); $data = $form->getData();
// do something with the data
return $app->redirect('/hello/{name}'); return $app->redirect('/hello/{name}');
} }
} }
return $app['twig']->render('index.twig', array('form' => $form->createView())); return $app['twig']->render('index.twig', array('form' => $form->createView()));
}) });
->method('GET|POST')
->bind('index');
Put this in your template file named ``views/index.twig``: Put this in your template file named ``views/index.twig``:
.. code-block:: jinja .. code-block:: jinja
<form action="{{ app.request.requestUri }}" method="post"> <form action="#" method="post">
{{ form_widget(form) }} {{ form_widget(form) }}
<input type="submit" name="submit" /> <input type="submit" name="submit" />
</form> </form>
You can also add validation to your form by creating a constraint and pass it If you are using the validator provider, you can also add validation to your
as the `validation_constraint` option:: form by adding constraints on the fields::
$validation_constraint = new Collection(array( use Symfony\Component\Validator\Constraints as Assert;
'name' => new NotBlank(),
));
$form = $app['form.factory']->createBuilder('form', null, array( $app->register(new Silex\Provider\ValidatorServiceProvider());
'validation_constraint' => $validation_constraint, $app->register(new Silex\Provider\TranslationServiceProvider(), array(
'translator.messages' => array(),
)); ));
$form = $app['form.factory']->createBuilder('form')
->add('name', 'text', array(
'constraints' => array(new Assert\NotBlank(), new Assert\MinLength(5))
))
->getForm();
For more information, consult the `Symfony2 Forms documentation For more information, consult the `Symfony2 Forms documentation
<http://symfony.com/doc/2.1/book/forms.html>`_. <http://symfony.com/doc/2.1/book/forms.html>`_.
...@@ -58,10 +58,11 @@ Validating values ...@@ -58,10 +58,11 @@ Validating values
You can validate values directly using the ``validateValue`` validator You can validate values directly using the ``validateValue`` validator
method:: method::
use Symfony\Component\Validator\Constraints; use Symfony\Component\Validator\Constraints as Assert;
$app->get('/validate-url', function () use ($app) { $app->get('/validate-url', function () use ($app) {
$violations = $app['validator']->validateValue($app['request']->get('url'), new Constraints\Url()); $violations = $app['validator']->validateValue($app['request']->get('url'), new Assert\Url());
return $violations; return $violations;
}); });
...@@ -76,7 +77,7 @@ you to define constraints for your object properties. It also works with ...@@ -76,7 +77,7 @@ you to define constraints for your object properties. It also works with
getters:: getters::
use Symfony\Component\Validator\Mapping\ClassMetadata; use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints; use Symfony\Component\Validator\Constraints as Assert;
class Post class Post
{ {
...@@ -85,9 +86,9 @@ getters:: ...@@ -85,9 +86,9 @@ getters::
static public function loadValidatorMetadata(ClassMetadata $metadata) static public function loadValidatorMetadata(ClassMetadata $metadata)
{ {
$metadata->addPropertyConstraint('title', new Constraints\NotNull()); $metadata->addPropertyConstraint('title', new Assert\NotNull());
$metadata->addPropertyConstraint('title', new Constraints\NotBlank()); $metadata->addPropertyConstraint('title', new Assert\NotBlank());
$metadata->addPropertyConstraint('body', new Constraints\MinLength(array('limit' => 10))); $metadata->addPropertyConstraint('body', new Assert\MinLength(array('limit' => 10)));
} }
} }
...@@ -97,6 +98,7 @@ getters:: ...@@ -97,6 +98,7 @@ getters::
$post->body = $app['request']->get('body'); $post->body = $app['request']->get('body');
$violations = $app['validator']->validate($post); $violations = $app['validator']->validate($post);
return $violations; return $violations;
}); });
......
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