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

@ -173,12 +173,12 @@ class Category extends Base
*/
public function validateCreation(array $values)
{
$v = new Validator($values, array(
$rules = array(
new Validators\Required('project_id', t('The project id is required')),
new Validators\Integer('project_id', t('The project id must be an integer')),
new Validators\Required('name', t('The name is required')),
new Validators\MaxLength('name', t('The maximum length is %d characters', 50), 50)
));
);
$v = new Validator($values, array_merge($rules, $this->commonValidationRules()));
return array(
$v->execute(),
@ -195,17 +195,31 @@ class Category extends Base
*/
public function validateModification(array $values)
{
$v = new Validator($values, array(
$rules = array(
new Validators\Required('id', t('The id is required')),
new Validators\Integer('id', t('The id must be an integer')),
new Validators\Integer('project_id', t('The project id must be an integer')),
new Validators\Required('name', t('The name is required')),
new Validators\MaxLength('name', t('The maximum length is %d characters', 50), 50)
));
);
$v = new Validator($values, array_merge($rules, $this->commonValidationRules()));
return array(
$v->execute(),
$v->getErrors()
);
}
/**
* Common validation rules
*
* @access private
* @return array
*/
private function commonValidationRules()
{
return array(
new Validators\Integer('id', t('The id must be an integer')),
new Validators\Integer('project_id', t('The project id must be an integer')),
new Validators\MaxLength('name', t('The maximum length is %d characters', 50), 50)
);
}
}

View File

@ -155,13 +155,12 @@ class Comment extends Base
*/
public function validateCreation(array $values)
{
$v = new Validator($values, array(
new Validators\Required('task_id', t('This value is required')),
new Validators\Integer('task_id', t('This value must be an integer')),
$rules = array(
new Validators\Required('user_id', t('This value is required')),
new Validators\Integer('user_id', t('This value must be an integer')),
new Validators\Required('comment', t('Comment is required'))
));
new Validators\Required('task_id', t('This value is required')),
);
$v = new Validator($values, array_merge($rules, $this->commonValidationRules()));
return array(
$v->execute(),
@ -178,15 +177,31 @@ class Comment extends Base
*/
public function validateModification(array $values)
{
$v = new Validator($values, array(
$rules = array(
new Validators\Required('id', t('This value is required')),
new Validators\Integer('id', t('This value must be an integer')),
new Validators\Required('comment', t('Comment is required'))
));
);
$v = new Validator($values, array_merge($rules, $this->commonValidationRules()));
return array(
$v->execute(),
$v->getErrors()
);
}
/**
* 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('task_id', t('This value must be an integer')),
new Validators\Integer('user_id', t('This value must be an integer')),
new Validators\Required('comment', t('Comment is required'))
);
}
}

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(),

View File

@ -234,7 +234,7 @@ class SubTask extends Base
new Validators\Required('title', t('The title is required')),
);
$v = new Validator($values, $rules + $this->commonValidationRules());
$v = new Validator($values, array_merge($rules, $this->commonValidationRules()));
return array(
$v->execute(),
@ -256,7 +256,7 @@ class SubTask extends Base
new Validators\Required('task_id', t('The task id is required')),
);
$v = new Validator($values, $rules + $this->commonValidationRules());
$v = new Validator($values, array_merge($rules, $this->commonValidationRules()));
return array(
$v->execute(),

View File

@ -674,6 +674,27 @@ class Task extends Base
return false;
}
/**
* 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('project_id', t('This value must be an integer')),
new Validators\Integer('column_id', t('This value must be an integer')),
new Validators\Integer('owner_id', t('This value must be an integer')),
new Validators\Integer('creator_id', t('This value must be an integer')),
new Validators\Integer('score', t('This value must be an integer')),
new Validators\Integer('category_id', t('This value must be an integer')),
new Validators\MaxLength('title', t('The maximum length is %d characters', 200), 200),
new Validators\Date('date_due', t('Invalid date'), $this->getDateFormats()),
);
}
/**
* Validate task creation
*
@ -683,17 +704,12 @@ class Task extends Base
*/
public function validateCreation(array $values)
{
$v = new Validator($values, array(
$rules = array(
new Validators\Required('project_id', t('The project is required')),
new Validators\Integer('project_id', t('This value must be an integer')),
new Validators\Integer('column_id', t('This value must be an integer')),
new Validators\Integer('owner_id', t('This value must be an integer')),
new Validators\Integer('creator_id', t('This value must be an integer')),
new Validators\Integer('score', t('This value must be an integer')),
new Validators\Required('title', t('The title is required')),
new Validators\MaxLength('title', t('The maximum length is %d characters', 200), 200),
new Validators\Date('date_due', t('Invalid date'), $this->getDateFormats()),
));
);
$v = new Validator($values, array_merge($rules, $this->commonValidationRules()));
return array(
$v->execute(),
@ -710,11 +726,12 @@ class Task extends Base
*/
public function validateDescriptionCreation(array $values)
{
$v = new Validator($values, array(
$rules = array(
new Validators\Required('id', t('The id is required')),
new Validators\Integer('id', t('This value must be an integer')),
new Validators\Required('description', t('The description is required')),
));
);
$v = new Validator($values, array_merge($rules, $this->commonValidationRules()));
return array(
$v->execute(),
@ -731,16 +748,11 @@ class Task extends Base
*/
public function validateModification(array $values)
{
$v = new Validator($values, array(
$rules = array(
new Validators\Required('id', t('The id is required')),
new Validators\Integer('id', t('This value must be an integer')),
new Validators\Integer('project_id', t('This value must be an integer')),
new Validators\Integer('column_id', t('This value must be an integer')),
new Validators\Integer('owner_id', t('This value must be an integer')),
new Validators\Integer('score', t('This value must be an integer')),
new Validators\MaxLength('title', t('The maximum length is %d characters', 200), 200),
new Validators\Date('date_due', t('Invalid date'), $this->getDateFormats()),
));
);
$v = new Validator($values, array_merge($rules, $this->commonValidationRules()));
return array(
$v->execute(),
@ -757,14 +769,13 @@ class Task extends Base
*/
public function validateAssigneeModification(array $values)
{
$v = new Validator($values, array(
$rules = array(
new Validators\Required('id', t('The id is required')),
new Validators\Integer('id', t('This value must be an integer')),
new Validators\Required('project_id', t('The project is required')),
new Validators\Integer('project_id', t('This value must be an integer')),
new Validators\Required('owner_id', t('This value is required')),
new Validators\Integer('owner_id', t('This value must be an integer')),
));
);
$v = new Validator($values, array_merge($rules, $this->commonValidationRules()));
return array(
$v->execute(),
@ -781,14 +792,14 @@ class Task extends Base
*/
public function validateCategoryModification(array $values)
{
$v = new Validator($values, array(
$rules = array(
new Validators\Required('id', t('The id is required')),
new Validators\Integer('id', t('This value must be an integer')),
new Validators\Required('project_id', t('The project is required')),
new Validators\Integer('project_id', t('This value must be an integer')),
new Validators\Required('category_id', t('This value is required')),
new Validators\Integer('category_id', t('This value must be an integer')),
));
);
$v = new Validator($values, array_merge($rules, $this->commonValidationRules()));
return array(
$v->execute(),
@ -805,12 +816,12 @@ class Task extends Base
*/
public function validateProjectModification(array $values)
{
$v = new Validator($values, array(
$rules = array(
new Validators\Required('id', t('The id is required')),
new Validators\Integer('id', t('This value must be an integer')),
new Validators\Required('project_id', t('The project is required')),
new Validators\Integer('project_id', t('This value must be an integer')),
));
);
$v = new Validator($values, array_merge($rules, $this->commonValidationRules()));
return array(
$v->execute(),

View File

@ -223,6 +223,40 @@ class User extends Base
$_SESSION['user'] = $user;
}
/**
* Common validation rules
*
* @access private
* @return array
*/
private function commonValidationRules()
{
return array(
new Validators\Required('username', t('The username is required')),
new Validators\MaxLength('username', t('The maximum length is %d characters', 50), 50),
new Validators\Unique('username', t('The username must be unique'), $this->db->getConnection(), self::TABLE, 'id'),
new Validators\Email('email', t('Email address invalid')),
new Validators\Integer('default_project_id', t('This value must be an integer')),
new Validators\Integer('is_admin', t('This value must be an integer')),
);
}
/**
* Common password validation rules
*
* @access private
* @return array
*/
private function commonPasswordValidationRules()
{
return array(
new Validators\Required('password', t('The password is required')),
new Validators\MinLength('password', t('The minimum length is %d characters', 6), 6),
new Validators\Required('confirmation', t('The confirmation is required')),
new Validators\Equals('password', 'confirmation', t('Passwords don\'t match')),
);
}
/**
* Validate user creation
*
@ -232,18 +266,7 @@ class User extends Base
*/
public function validateCreation(array $values)
{
$v = new Validator($values, array(
new Validators\Required('username', t('The username is required')),
new Validators\MaxLength('username', t('The maximum length is %d characters', 50), 50),
new Validators\Unique('username', t('The username must be unique'), $this->db->getConnection(), self::TABLE, 'id'),
new Validators\Required('password', t('The password is required')),
new Validators\MinLength('password', t('The minimum length is %d characters', 6), 6),
new Validators\Required('confirmation', t('The confirmation is required')),
new Validators\Equals('password', 'confirmation', t('Passwords don\'t match')),
new Validators\Integer('default_project_id', t('This value must be an integer')),
new Validators\Integer('is_admin', t('This value must be an integer')),
new Validators\Email('email', t('Email address invalid')),
));
$v = new Validator($values, array_merge($this->commonValidationRules(), $this->commonPasswordValidationRules()));
return array(
$v->execute(),
@ -260,19 +283,11 @@ class User extends Base
*/
public function validateModification(array $values)
{
if (! empty($values['password'])) {
return $this->validatePasswordModification($values);
}
$v = new Validator($values, array(
$rules = array(
new Validators\Required('id', t('The user id is required')),
new Validators\Required('username', t('The username is required')),
new Validators\MaxLength('username', t('The maximum length is %d characters', 50), 50),
new Validators\Unique('username', t('The username must be unique'), $this->db->getConnection(), self::TABLE, 'id'),
new Validators\Integer('default_project_id', t('This value must be an integer')),
new Validators\Integer('is_admin', t('This value must be an integer')),
new Validators\Email('email', t('Email address invalid')),
));
);
$v = new Validator($values, array_merge($rules, $this->commonValidationRules()));
return array(
$v->execute(),
@ -289,14 +304,12 @@ class User extends Base
*/
public function validatePasswordModification(array $values)
{
$v = new Validator($values, array(
$rules = array(
new Validators\Required('id', t('The user id is required')),
new Validators\Required('current_password', t('The current password is required')),
new Validators\Required('password', t('The password is required')),
new Validators\MinLength('password', t('The minimum length is %d characters', 6), 6),
new Validators\Required('confirmation', t('The confirmation is required')),
new Validators\Equals('password', 'confirmation', t('Passwords don\'t match')),
));
);
$v = new Validator($values, array_merge($rules, $this->commonPasswordValidationRules()));
if ($v->execute()) {