Completed
Push — master ( f39c4d...b2e354 )
by Sam
03:35 queued 03:17
created

Validator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\Forms;
4
5
use SilverStripe\Core\Object;
6
use SilverStripe\ORM\ValidationResult;
7
8
/**
9
 * This validation class handles all form and custom form validation through the use of Required
10
 * fields. It relies on javascript for client-side validation, and marking fields after server-side
11
 * validation. It acts as a visitor to individual form fields.
12
 */
13
abstract class Validator extends Object
14
{
15
16
    public function __construct()
17
    {
18
        parent::__construct();
19
        $this->resetResult();
20
    }
21
22
    /**
23
     * @var Form $form
24
     */
25
    protected $form;
26
27
    /**
28
     * @var ValidationResult $result
29
     */
30
    protected $result;
31
32
    /**
33
     * @param Form $form
34
     * @return $this
35
     */
36
    public function setForm($form)
37
    {
38
        $this->form = $form;
39
        return $this;
40
    }
41
42
    /**
43
     * Returns any errors there may be.
44
     *
45
     * @return ValidationResult
46
     */
47
    public function validate()
48
    {
49
        $this->resetResult();
50
        $this->php($this->form->getData());
51
        return $this->result;
52
    }
53
54
    /**
55
     * Callback to register an error on a field (Called from implementations of
56
     * {@link FormField::validate}). The optional error message type parameter is loaded into the
57
     * HTML class attribute.
58
     *
59
     * See {@link getErrors()} for details.
60
     *
61
     * @param string $fieldName Field name for this error
62
     * @param string $message The message string
63
     * @param string $messageType The type of message: e.g. "bad", "warning", "good", or "required". Passed as a CSS
64
     *                            class to the form, so other values can be used if desired.
65
     * @param string|bool $cast Cast type; One of the CAST_ constant definitions.
66
     * Bool values will be treated as plain text flag.
67
     * @return $this
68
     */
69
    public function validationError(
70
        $fieldName,
71
        $message,
72
        $messageType = ValidationResult::TYPE_ERROR,
73
        $cast = ValidationResult::CAST_TEXT
74
    ) {
75
        $this->result->addFieldError($fieldName, $message, $messageType, null, $cast);
76
        return $this;
77
    }
78
79
    /**
80
     * Returns all errors found by a previous call to {@link validate()}. The returned array has a
81
     * structure resembling:
82
     *
83
     * <code>
84
     *     array(
85
     *         'fieldName' => '[form field name]',
86
     *         'message' => '[validation error message]',
87
     *         'messageType' => '[bad|message|validation|required]',
88
     *         'messageCast' => '[text|html]'
89
     *     )
90
     * </code>
91
     *
92
     * @return null|array
93
     */
94
    public function getErrors()
95
    {
96
        if ($this->result) {
97
            return $this->result->getMessages();
98
        }
99
        return null;
100
    }
101
102
    /**
103
     * Get last validation result
104
     *
105
     * @return ValidationResult
106
     */
107
    public function getResult()
108
    {
109
        return $this->result;
110
    }
111
112
    /**
113
     * Returns whether the field in question is required. This will usually display '*' next to the
114
     * field. The base implementation always returns false.
115
     *
116
     * @param string $fieldName
117
     *
118
     * @return bool
119
     */
120
    public function fieldIsRequired($fieldName)
121
    {
122
        return false;
123
    }
124
125
    /**
126
     * @param array $data
127
     *
128
     * @return mixed
129
     */
130
    abstract public function php($data);
131
132
    /**
133
     * Clear current result
134
     *
135
     * @return $this
136
     */
137
    protected function resetResult()
138
    {
139
        $this->result = ValidationResult::create();
140
        return $this;
141
    }
142
}
143