Remove some code duplication (validation rules)
This commit is contained in:
@@ -173,12 +173,12 @@ class Category extends Base
|
|||||||
*/
|
*/
|
||||||
public function validateCreation(array $values)
|
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\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\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(
|
return array(
|
||||||
$v->execute(),
|
$v->execute(),
|
||||||
@@ -195,17 +195,31 @@ class Category extends Base
|
|||||||
*/
|
*/
|
||||||
public function validateModification(array $values)
|
public function validateModification(array $values)
|
||||||
{
|
{
|
||||||
$v = new Validator($values, array(
|
$rules = array(
|
||||||
new Validators\Required('id', t('The id is required')),
|
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\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(
|
return array(
|
||||||
$v->execute(),
|
$v->execute(),
|
||||||
$v->getErrors()
|
$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)
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -155,13 +155,12 @@ class Comment extends Base
|
|||||||
*/
|
*/
|
||||||
public function validateCreation(array $values)
|
public function validateCreation(array $values)
|
||||||
{
|
{
|
||||||
$v = new Validator($values, array(
|
$rules = array(
|
||||||
new Validators\Required('task_id', t('This value is required')),
|
|
||||||
new Validators\Integer('task_id', t('This value must be an integer')),
|
|
||||||
new Validators\Required('user_id', t('This value is required')),
|
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('task_id', t('This value is required')),
|
||||||
new Validators\Required('comment', t('Comment is required'))
|
);
|
||||||
));
|
|
||||||
|
$v = new Validator($values, array_merge($rules, $this->commonValidationRules()));
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
$v->execute(),
|
$v->execute(),
|
||||||
@@ -178,15 +177,31 @@ class Comment extends Base
|
|||||||
*/
|
*/
|
||||||
public function validateModification(array $values)
|
public function validateModification(array $values)
|
||||||
{
|
{
|
||||||
$v = new Validator($values, array(
|
$rules = array(
|
||||||
new Validators\Required('id', t('This value is required')),
|
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(
|
return array(
|
||||||
$v->execute(),
|
$v->execute(),
|
||||||
$v->getErrors()
|
$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'))
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -623,6 +623,23 @@ class Project extends Base
|
|||||||
->save(array('is_public' => 0, 'token' => ''));
|
->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
|
* Validate project creation
|
||||||
*
|
*
|
||||||
@@ -632,11 +649,7 @@ class Project extends Base
|
|||||||
*/
|
*/
|
||||||
public function validateCreation(array $values)
|
public function validateCreation(array $values)
|
||||||
{
|
{
|
||||||
$v = new Validator($values, array(
|
$v = new Validator($values, $this->commonValidationRules());
|
||||||
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)
|
|
||||||
));
|
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
$v->execute(),
|
$v->execute(),
|
||||||
@@ -653,14 +666,11 @@ class Project extends Base
|
|||||||
*/
|
*/
|
||||||
public function validateModification(array $values)
|
public function validateModification(array $values)
|
||||||
{
|
{
|
||||||
$v = new Validator($values, array(
|
$rules = array(
|
||||||
new Validators\Required('id', t('The project id is required')),
|
new Validators\Required('id', t('This value 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),
|
$v = new Validator($values, array_merge($rules, $this->commonValidationRules()));
|
||||||
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'))
|
|
||||||
));
|
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
$v->execute(),
|
$v->execute(),
|
||||||
|
|||||||
@@ -234,7 +234,7 @@ class SubTask extends Base
|
|||||||
new Validators\Required('title', t('The title is required')),
|
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(
|
return array(
|
||||||
$v->execute(),
|
$v->execute(),
|
||||||
@@ -256,7 +256,7 @@ class SubTask extends Base
|
|||||||
new Validators\Required('task_id', t('The task id is required')),
|
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(
|
return array(
|
||||||
$v->execute(),
|
$v->execute(),
|
||||||
|
|||||||
@@ -674,6 +674,27 @@ class Task extends Base
|
|||||||
return false;
|
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
|
* Validate task creation
|
||||||
*
|
*
|
||||||
@@ -683,17 +704,12 @@ class Task extends Base
|
|||||||
*/
|
*/
|
||||||
public function validateCreation(array $values)
|
public function validateCreation(array $values)
|
||||||
{
|
{
|
||||||
$v = new Validator($values, array(
|
$rules = array(
|
||||||
new Validators\Required('project_id', t('The project is required')),
|
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\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(
|
return array(
|
||||||
$v->execute(),
|
$v->execute(),
|
||||||
@@ -710,11 +726,12 @@ class Task extends Base
|
|||||||
*/
|
*/
|
||||||
public function validateDescriptionCreation(array $values)
|
public function validateDescriptionCreation(array $values)
|
||||||
{
|
{
|
||||||
$v = new Validator($values, array(
|
$rules = array(
|
||||||
new Validators\Required('id', t('The id is required')),
|
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')),
|
new Validators\Required('description', t('The description is required')),
|
||||||
));
|
);
|
||||||
|
|
||||||
|
$v = new Validator($values, array_merge($rules, $this->commonValidationRules()));
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
$v->execute(),
|
$v->execute(),
|
||||||
@@ -731,16 +748,11 @@ class Task extends Base
|
|||||||
*/
|
*/
|
||||||
public function validateModification(array $values)
|
public function validateModification(array $values)
|
||||||
{
|
{
|
||||||
$v = new Validator($values, array(
|
$rules = array(
|
||||||
new Validators\Required('id', t('The id is required')),
|
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')),
|
$v = new Validator($values, array_merge($rules, $this->commonValidationRules()));
|
||||||
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()),
|
|
||||||
));
|
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
$v->execute(),
|
$v->execute(),
|
||||||
@@ -757,14 +769,13 @@ class Task extends Base
|
|||||||
*/
|
*/
|
||||||
public function validateAssigneeModification(array $values)
|
public function validateAssigneeModification(array $values)
|
||||||
{
|
{
|
||||||
$v = new Validator($values, array(
|
$rules = array(
|
||||||
new Validators\Required('id', t('The id is required')),
|
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\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\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(
|
return array(
|
||||||
$v->execute(),
|
$v->execute(),
|
||||||
@@ -781,14 +792,14 @@ class Task extends Base
|
|||||||
*/
|
*/
|
||||||
public function validateCategoryModification(array $values)
|
public function validateCategoryModification(array $values)
|
||||||
{
|
{
|
||||||
$v = new Validator($values, array(
|
$rules = array(
|
||||||
new Validators\Required('id', t('The id is required')),
|
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\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\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(
|
return array(
|
||||||
$v->execute(),
|
$v->execute(),
|
||||||
@@ -805,12 +816,12 @@ class Task extends Base
|
|||||||
*/
|
*/
|
||||||
public function validateProjectModification(array $values)
|
public function validateProjectModification(array $values)
|
||||||
{
|
{
|
||||||
$v = new Validator($values, array(
|
$rules = array(
|
||||||
new Validators\Required('id', t('The id is required')),
|
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\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(
|
return array(
|
||||||
$v->execute(),
|
$v->execute(),
|
||||||
|
|||||||
@@ -223,6 +223,40 @@ class User extends Base
|
|||||||
$_SESSION['user'] = $user;
|
$_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
|
* Validate user creation
|
||||||
*
|
*
|
||||||
@@ -232,18 +266,7 @@ class User extends Base
|
|||||||
*/
|
*/
|
||||||
public function validateCreation(array $values)
|
public function validateCreation(array $values)
|
||||||
{
|
{
|
||||||
$v = new Validator($values, array(
|
$v = new Validator($values, array_merge($this->commonValidationRules(), $this->commonPasswordValidationRules()));
|
||||||
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')),
|
|
||||||
));
|
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
$v->execute(),
|
$v->execute(),
|
||||||
@@ -260,19 +283,11 @@ class User extends Base
|
|||||||
*/
|
*/
|
||||||
public function validateModification(array $values)
|
public function validateModification(array $values)
|
||||||
{
|
{
|
||||||
if (! empty($values['password'])) {
|
$rules = array(
|
||||||
return $this->validatePasswordModification($values);
|
|
||||||
}
|
|
||||||
|
|
||||||
$v = new Validator($values, array(
|
|
||||||
new Validators\Required('id', t('The user id is required')),
|
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'),
|
$v = new Validator($values, array_merge($rules, $this->commonValidationRules()));
|
||||||
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')),
|
|
||||||
));
|
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
$v->execute(),
|
$v->execute(),
|
||||||
@@ -289,14 +304,12 @@ class User extends Base
|
|||||||
*/
|
*/
|
||||||
public function validatePasswordModification(array $values)
|
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('id', t('The user id is required')),
|
||||||
new Validators\Required('current_password', t('The current password 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')),
|
$v = new Validator($values, array_merge($rules, $this->commonPasswordValidationRules()));
|
||||||
new Validators\Equals('password', 'confirmation', t('Passwords don\'t match')),
|
|
||||||
));
|
|
||||||
|
|
||||||
if ($v->execute()) {
|
if ($v->execute()) {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user