Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Sign in
Toggle navigation
S
Silex
Project overview
Project overview
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
common
Silex
Commits
ce382188
Commit
ce382188
authored
Jun 15, 2012
by
Fabien Potencier
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed documentation for forms
parent
82fa4ef8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
27 additions
and
38 deletions
+27
-38
doc/providers/form.rst
doc/providers/form.rst
+19
-32
doc/providers/validator.rst
doc/providers/validator.rst
+8
-6
No files found.
doc/providers/form.rst
View file @
ce382188
...
...
@@ -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>`_.
doc/providers/validator.rst
View file @
ce382188
...
...
@@ -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;
});
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment