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