a SensioLabs Product

The PHP micro-framework
based on the Symfony Components

You are reading the documentation for Silex 2.0. Switch to the documentation for Silex 1.3.

Questions & Feedback

License

Creative Commons License Silex documentation is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License.

Using 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:

1
composer require symfony/yaml

Next, you need to tell the Validation Service that you are not using StaticMethodLoader to load your class metadata but a YAML file:

1
2
3
4
5
$app->register(new ValidatorServiceProvider());

$app['validator.mapping.class_metadata_factory'] = new Symfony\Component\Validator\Mapping\Factory\LazyLoadingMetadataFactory(
    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:

1
2
3
4
5
6
7
8
# validation.yml
Post:
  properties:
    title:
      - NotNull: ~
      - NotBlank: ~
    body:
      - Min: 100
Website powered by Symfony and Twig, deployed on
The Silex logo is © 2010-2017 SensioLabs