Commit e3113f97 authored by Fabien Potencier's avatar Fabien Potencier

moved previous doc changes to a cookbook

parent 1fbce521
...@@ -11,6 +11,7 @@ The cookbook section contains recipes for solving specific problems. ...@@ -11,6 +11,7 @@ The cookbook section contains recipes for solving specific problems.
translating_validation_messages translating_validation_messages
session_storage session_storage
form_no_csrf form_no_csrf
validator_yaml
Recipes Recipes
------- -------
...@@ -24,3 +25,5 @@ Recipes ...@@ -24,3 +25,5 @@ Recipes
* :doc:`How to use PdoSessionStorage to store sessions in the database <session_storage>`. * :doc:`How to use PdoSessionStorage to store sessions in the database <session_storage>`.
* :doc:`How to disable the CSRF Protection on a form using the FormExtension <form_no_csrf>`. * :doc:`How to disable the CSRF Protection on a form using the FormExtension <form_no_csrf>`.
* :doc:`How to use YAML to configure validation <validator_yaml>`.
How to use YAML to configure validation
=======================================
Simplicity is at the heart of Silex so there is no out of the box solution to
use YAML files for validation. But this doesn't mean that this is not
possible. Let's see how to do it.
First, you need to install the YAML Component. Declare it as a dependency in
your ``composer.json`` file:
.. code-block:: json
"require": {
"symfony/yaml": "2.1.*"
}
Next, you need to tell the Validation Service that you are not using
``StaticMethodLoader`` to load your class metadata but a YAML file:
.. code-block:: php
$app->register(new ValidatorServiceProvider());
$app['validator.mapping.class_metadata_factory'] = new Symfony\Component\Validator\Mapping\ClassMetadataFactory(
new Symfony\Component\Validator\Mapping\Loader\YamlFileLoader(__DIR__.'/validation.yml')
);
Now, we can replace the usage of the static method and move all the validation
rules to ``validation.yml``:
.. code-block:: yaml
# validation.yml
Post:
properties:
title:
- NotNull: ~
- NotBlank: ~
body:
- Min: 100
...@@ -104,37 +104,3 @@ however use the *FormServiceProvider* which can make use of the *ValidatorServic ...@@ -104,37 +104,3 @@ however use the *FormServiceProvider* which can make use of the *ValidatorServic
For more information, consult the `Symfony2 Validation documentation For more information, consult the `Symfony2 Validation documentation
<http://symfony.com/doc/2.0/book/validation.html>`_. <http://symfony.com/doc/2.0/book/validation.html>`_.
Validation in YAML files
~~~~~~~~~~~~~~~~~~~~~~~~
Simplicity is at the heart of Silex so there is no out of the box solution to use YAML files for validation.
But this doesn't mean that this is not possible. Let's see how to do it.
First, you need to install the YAML Component. If you want to take advantage of the autoloading mechanism you should clone https://github.com/symfony/Yaml into ``vendor/Symfony/Component``.
Next, you need to tell the Validation Service that you are not using StaticMethodLoader to load your class metadata
but a YAML file
.. code-block:: php
$app->register(new ValidatorServiceProvider());
$app['validator.mapping.class_metadata_factory'] = new Symfony\Component\Validator\Mapping\ClassMetadataFactory(
new Symfony\Component\Validator\Mapping\Loader\YamlFileLoader(__DIR__.'/validation.yml')
);
Now, we can remove the static method in the ``Post`` class and move all these rules to ``validation.yml``
.. code-block:: yaml
# validation.yml
Post:
properties:
title:
- NotNull: ~
- NotBlank: ~
body:
- Min: 100
And that's all. Now on you can use the Validator service as defined above.
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