Using uniqueness validation in forms with multiple fields and specifying values

Hi,

When using the uniqueness validation in a form and using the field option, is it possible to specify what one of the field values are?

As an example, users are allowed to add multiple keywords to their account but can only add a keyword once per user. Putting validation in the model would allow me to test against the user, but is there a way I can do it in forms?

<?php

use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;
use Phalcon\Validation\Validator;

class KeywordForm extends Form
{
    public function initialize()
    {
        $this->setEntity($this);

        $keyword = new Text('keyword');
        $keyword->setFilters([
            'lower',
            'striptags',
            'string',
            'trim',
        ]);
        $keyword->addValidators([
            new Validator\PresenceOf([
                'message' => 'Keyword is required',
            ]),
            new Validator\Uniqueness([
                'model' => new Keyword(),
                'field' => [
                    'userId',
                    'keyword',
                ],
                // not sure if something like this is possible?
                'values' => [
                    'userId' => 1,
                ],
                'message' => 'Keyword already exists',
            ]),
        ]);
        $this->add($keyword);
    }
}

Thanks, Gary

edited Mar '17

Hmmmmm this is good question, atm validation like this is only possible when:

`$validation->add(['userId, 'keyword'], new Uniquness([
    'model' => new Keyword(),
    'message' => 'Keyword already exists',
))`

I think it's possible to create your custom validator like:

class FormValidation extends Validation 
{
    public function initialize()
    {
        $this->add(['userId', 'keyword'], new Uniqueness([
            'model' => new Keyword(),
        ]))
    }
}

And then in you form:

class KeywordForm extens Form
{
    public functin getValidation()
    {
        return new FormValidation();
    }

    public function initialize()
    {
        $this->setEntity($this);

        $keyword = new Text('keyword');
        $keyword->setFilters([
            'lower',
            'striptags',
            'string',
            'trim',
        ]);
        $keyword->addValidators([
            new Validator\PresenceOf([
                'message' => 'Keyword is required',
            ]),
        ]);
        $this->add($keyword);
    }
}

Also there i no such a key as values. Also you sure you want to setEntity to $this? Entity should be most likely a model.

I figured values wasn't a thing, was more an example.
I've been setting it to $this when creating new objects, I guess I might be better off using new Keyword() or something similar?

edited Mar '17

I think so, but im not using forms, check docs - https://docs.phalconphp.com/pl/latest/reference/forms.html#forms-entities

Also notice if you will use Keyword as entity you won't need to set "model" => new Keyword()