Vegas CMF Validation

Vegas\Validation\Validator\Unique

Unique validator is our version of Phalcon\Validation\Validator\Uniqueness with some extra options and Validator Trait implemented.

Simple setup

By default, field searched in model given by modelName option will be the same as form element name. Default used adapter is Mongo Adapter.

namespace Foo\Forms;

use Phalcon\Forms\Element\Text;
use Vegas\Validation\Validator\Unique;

class Bar extends \Vegas\Forms\Form
{
    public function initialize()
    {
        $email = new Text('email');
        $email->addValidator(new Unique(['modelName' => '\My\Model\Name']));
        $this->add($email);

        // ...
    }
}

Full setup

namespace Foo\Forms;

use Phalcon\Forms\Element\Text;
use Vegas\Validation\Validator\Unique;

class Bar extends \Vegas\Forms\Form
{
    public function initialize()
    {
        $email = new Text('email');
        $email->addValidator(new Unique([
            'modelName' => '\My\Model\Name',
            'fieldName' => 'username',
            'adapter' => new \Vegas\Validation\Validator\Unique\Adapter\Mongo(),
            'message' => $this->i18n->_('User already exists in our database!')
        ]));
        $this->add($email);

        // ...
    }
}