polifonic / twig-validator-extension
A Twig extension to validate objects inside a Twig template
Installs: 8 632
Dependents: 0
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: >=5.5.9
- twig/twig: ~1.12
Requires (Dev)
- phpunit/phpunit: ~4.8||~5.4
- symfony/validator: ^2.5||^3.0
This package is not auto-updated.
Last update: 2024-11-09 20:56:15 UTC
README
A simple Twig extension that adds a valid
filter to be used in twig templates.
With this filter you can test if objects are valid inside a twig template and generate the appropriate markup based on the result.
Installation
Add the package to your app's composer.json
:
"require": { "polifonic/twig-validator-extension": "^1.0", }
As a Twig Extension
Create an instance of TwigValidatorExtension
and add it to the Twig environment just like any other twig extension.
The TwigValidatorExtension
constructor needs to be passed a validator (an instance of Symfony\Component\Validator\Validator\ValidatorInterface
).
use Polifonic\Twig\Extension\Validator\TwigValidatorExtension; $validator = ...; $twig = new Twig_Environment($loader); $twig->addExtension(new TwigValidatorExtension($validator));
As a Symfony bundle
The package includes a Symfony bundle named TwigValidatorBundle
. This bundle
will automatically add the TwigValidatorExtension
to twig.
Enable the TwigValidatorBundle
symfony bundle by adding it to your app's kernel:
# app/AppKernel.php public function regsiterBundles() { $bundles = array( ... new Polifonic\Twig\Extension\Validator\Symfony\TwigValidatorBundle(), ); }
Usage
{% if object|valid %}...{% endif %}
With validation groups:
{% if object|valid([ "group1", "group2" ]) %}...{% endif %}