/** * {@inheritdoc} * * @return void */ public function beforeSave() { // nickname validator $this->addValidator('nickname', v::required()); // group validator $this->addValidator('group', v::required()); // types validator $this->types = explode(',', $this->types); $this->addValidator('types', v::callback(function ($input) { foreach ($input as &$type) { $type = trim($type); } $length = sizeof($input); $types = \Application\MusicTypes\Table::select()->select('type')->where('type IN (?)', $input)->execute(); if (sizeof($types) == $length) { return true; } })->setError('Types does not exist')); // concert_date validator $this->addValidator('concertDate', v::required(), v::date('Y-m-d')); $this->addValidator('alias', v::required(), v::length(2, 64), v::slug(), v::callback(function ($input) { $select = $this->getTable()->select()->where('alias = ?', $input); if ($this->id) { $select->andWhere('id != ?', $this->id); } return !sizeof($select->execute()); })->setError('Musician with alias "{{input}}" already exists')); }
/** * {@inheritdoc} */ protected function beforeInsert() { // unique validator $this->addValidator('key', v::callback(function () { return !$this->getTable()->findRowWhere(['key' => $this->key, 'namespace' => $this->namespace]); })->setError('Key name "{{input}}" already exists')); }
/** * Setup custom error text for many rules in chain */ public function testSetCustomErrorTextForValidatorChainShouldUseItAsErrorMessage() { try { Validator::callback('is_int')->callback('is_numeric')->setName('Input')->setError('"{{name}}" is not numeric, is equal "{{input}}"')->assert('something'); } catch (\Exception $e) { $this->assertEquals('"Input" is not numeric, is equal "something"', $e->getMessage()); } }
/** * Before Update * @return void */ protected function beforeUpdate() { $this->addValidator('name', v::required()->latin(), v::callback(function ($name) { return !in_array(strtolower($name), Table::getInstance()->getBasicRoles()); })->setError('Role "{{input}}" is basic and can\'t be editable'), v::callback(function ($name) { return $this->clean['name'] != $name; })->setError('Role name "{{input}}" the same as original'), v::callback(function ($name) { return !Table::getInstance()->findRowWhere(['name' => $name]); })->setError('Role name "{{input}}" already exists')); }
/** * @return void */ public function beforeSave() { $this->email = strtolower($this->email); $this->addValidator('login', v::required()->latin()->length(3, 255), v::callback(function ($login) { $user = $this->getTable()->select()->where('login = ?', $login)->andWhere('id != ?', $this->id)->execute(); return !$user; })->setError('User with login "{{input}}" already exists')); $this->addValidator('email', v::required()->email(true), v::callback(function ($email) { $user = $this->getTable()->select()->where('email = ?', $email)->andWhere('id != ?', $this->id)->execute(); return !$user; })->setError('User with email "{{input}}" already exists')); }
/** * Setup multi builder for object */ public function testValidatorBuilderForRuleSetAndObject() { $object = new \stdClass(); $object->foo = 0; $object->bar = 42; $object->pass = '******'; $validator = new ValidatorBuilder(); $validator->add('foo', Validator::numeric()); $validator->add('bar', Validator::required()); $validator->add('bar', Validator::callback('is_int')); $validator->add('quz', Validator::numeric()); $this->assertTrue($validator->validate($object)); $this->assertTrue($validator->assert($object)); }
/** * @return void */ public function beforeSave() { $this->addValidator('name', v::required(), v::length(2, 128)); $this->addValidator('alias', v::required(), v::length(2, 64), v::slug(), v::callback(function ($input) { $select = $this->getTable()->select()->where('alias = ?', $input); if ($this->id) { $select->andWhere('id != ?', $this->id); } if ($this->parentId) { $select->andWhere('parentId = ?', $this->parentId); } else { $select->andWhere('parentId IS NULL'); } return !sizeof($select->execute()); })->setError('Category with alias "{{input}}" already exists')); }
/** * {@inheritdoc} * * @return void */ public function beforeSave() { // title validator $this->addValidator('title', v::required()); // alias validator $this->addValidator('alias', v::required(), v::slug(), v::callback(function ($input) { if ($row = $this->getTable()->findRowWhere(['alias' => $input])) { if ($row->id != $this->id) { return false; } } return true; })->setError('Alias "{{input}}" already exists')); // content validator $this->addValidator('content', v::callback(function ($input) { if (empty($input) or trim(strip_tags($input, '<img>')) == '') { return false; } return true; })->setError('Content can\'t be empty')); }