Commit ce382188 authored by Fabien Potencier's avatar Fabien Potencier

fixed documentation for forms

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