summaryrefslogtreecommitdiff
path: root/app/Validator/ProjectValidator.php
blob: 8d8053a34a324a2f847b8f0676958a4709af3f89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php

namespace Kanboard\Validator;

use SimpleValidator\Validator;
use SimpleValidator\Validators;
use Kanboard\Model\ProjectModel;

/**
 * Project Validator
 *
 * @package  Kanboard\Validator
 * @author   Frederic Guillot
 */
class ProjectValidator extends BaseValidator
{
    /**
     * 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('priority_default', t('This value must be an integer')),
            new Validators\Integer('priority_start', t('This value must be an integer')),
            new Validators\Integer('priority_end', t('This value must be an integer')),
            new Validators\Integer('is_active', t('This value must be an integer')),
            new Validators\MaxLength('name', t('The maximum length is %d characters', 50), 50),
            new Validators\MaxLength('identifier', t('The maximum length is %d characters', 50), 50),
            new Validators\MaxLength('start_date', t('The maximum length is %d characters', 10), 10),
            new Validators\MaxLength('end_date', t('The maximum length is %d characters', 10), 10),
            new Validators\AlphaNumeric('identifier', t('This value must be alphanumeric')) ,
            new Validators\Unique('identifier', t('The identifier must be unique'), $this->db->getConnection(), ProjectModel::TABLE),
            new Validators\Email('email', t('Email address invalid')) ,
            new Validators\Unique('email', t('The email project must be unique across all projects'), $this->db->getConnection(), ProjectModel::TABLE),
        );
    }

    /**
     * Validate project creation
     *
     * @access public
     * @param  array   $values           Form values
     * @return array   $valid, $errors   [0] = Success or not, [1] = List of errors
     */
    public function validateCreation(array $values)
    {
        if (! empty($values['identifier'])) {
            $values['identifier'] = strtoupper($values['identifier']);
        }

        $rules = array(
            new Validators\Required('name', t('The project name is required')),
        );

        $v = new Validator($values, array_merge($rules, $this->commonValidationRules()));

        return array(
            $v->execute(),
            $v->getErrors()
        );
    }

    /**
     * Validate project modification
     *
     * @access public
     * @param  array   $values           Form values
     * @return array   $valid, $errors   [0] = Success or not, [1] = List of errors
     */
    public function validateModification(array $values)
    {
        if (! empty($values['identifier'])) {
            $values['identifier'] = strtoupper($values['identifier']);
        }

        $rules = array(
            new Validators\NotEmpty('name', t('This field cannot be empty')),
            new Validators\Required('id', t('This value is required')),
        );

        $v = new Validator($values, array_merge($rules, $this->commonValidationRules()));

        return array(
            $v->execute(),
            $v->getErrors()
        );
    }
}