majkl578/nette-symfony-validator

Integration of Symfony Validator component into Nette 2.

Installs: 39

Dependents: 0

Stars: 5

Watchers: 2

Forks: 1

Language: PHP

1.1.0 2013-04-12 20:00 UTC

README

Nette addon for integrating Symfony Validator component into Nette 2.

Requirements

  • PHP 5.3.3 or newer
  • Symfony Validator 2.2
  • Doctrine Common 2.3
  • Nette 2.0
  • Doctrine ORM 2.3 (optional)

Installation

  • Add "majkl578/nette-symfony-validator" to your dependencies in composer.json. Don't forget to run composer update.
  • Register extension to start using this addon. Add the following call just before the call $configurator->createContainer():
Majkl578\NetteSymfonyValidator\Config\Extension::register($configurator);
  • If you want to use integration with Doctrine ORM, register also ORM extension:
Majkl578\NetteSymfonyValidator\Config\ORMExtension::register($configurator);
  • Delete cache.

You're done. ;)

General usage

Just use as normal service, e.g. autowire Symfony\Component\Validator\Validator. For more general usage, see Symfony 2 Validation documentation.

Doctrine 2 ORM

Validation is performed automatically only on new or changed entities. The validation is registered automatically through event listener. It is listening for preFlush event so the validation itself is performed after calling EntityManager::flush(), just before committing changes to database.

For more information about adding constraints to fields, see Properties in Symfony 2 Validation documentation. Here's just a simple example:

namespace Example;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

class User
{
    /**
     * @ORM\Column
     * @Assert\NotBlank
     */
    private $name;

    /**
     * @ORM\Column
     * @Assert\NotBlank
     * @Assert\Email
     */
    private $email;

    // ...
}

Issues

In case of any problems, just leave an issue here on GitHub (or, better, send a pull request).