polifonic/twig-validator-extension

A Twig extension to validate objects inside a Twig template

1.0.3 2016-11-04 12:01 UTC

This package is not auto-updated.

Last update: 2024-04-13 17:26:05 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 %}