Remove some code duplication (validation rules)

This commit is contained in:
Frédéric Guillot
2014-09-12 15:57:05 +02:00
parent 15e1ed6148
commit c3a0cf4343
6 changed files with 162 additions and 99 deletions

View File

@@ -623,6 +623,23 @@ class Project extends Base
->save(array('is_public' => 0, 'token' => ''));
}
/**
* Common validation rules
*
* @access private
* @return array
*/
private function commonValidationRules()
{
return array(
new Validators\Integer('id', t('This value must be an integer')),
new Validators\Integer('is_active', t('This value must be an integer')),
new Validators\Required('name', t('The project name is required')),
new Validators\MaxLength('name', t('The maximum length is %d characters', 50), 50),
new Validators\Unique('name', t('This project must be unique'), $this->db->getConnection(), self::TABLE),
);
}
/**
* Validate project creation
*
@@ -632,11 +649,7 @@ class Project extends Base
*/
public function validateCreation(array $values)
{
$v = new Validator($values, array(
new Validators\Required('name', t('The project name is required')),
new Validators\MaxLength('name', t('The maximum length is %d characters', 50), 50),
new Validators\Unique('name', t('This project must be unique'), $this->db->getConnection(), self::TABLE)
));
$v = new Validator($values, $this->commonValidationRules());
return array(
$v->execute(),
@@ -653,14 +666,11 @@ class Project extends Base
*/
public function validateModification(array $values)
{
$v = new Validator($values, array(
new Validators\Required('id', t('The project id is required')),
new Validators\Integer('id', t('This value must be an integer')),
new Validators\Required('name', t('The project name is required')),
new Validators\MaxLength('name', t('The maximum length is %d characters', 50), 50),
new Validators\Unique('name', t('This project must be unique'), $this->db->getConnection(), self::TABLE),
new Validators\Integer('is_active', t('This value must be an integer'))
));
$rules = array(
new Validators\Required('id', t('This value is required')),
);
$v = new Validator($values, array_merge($rules, $this->commonValidationRules()));
return array(
$v->execute(),