Completed
Push — fix-2494 ( 3153ee...40d9bb )
by Sam
13:43 queued 06:38
created

Validator::getResult()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
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
     * @var bool
34
     */
35
    private $enabled = true;
36
37
    /**
38
     * @param Form $form
39
     * @return $this
40
     */
41
    public function setForm($form)
42
    {
43
        $this->form = $form;
44
        return $this;
45
    }
46
47
    /**
48
     * Returns any errors there may be.
49
     *
50
     * @return ValidationResult
51
     */
52
    public function validate()
53
    {
54
        $this->resetResult();
55
        if ($this->getEnabled()) {
56
            $this->php($this->form->getData());
57
        }
58
        return $this->result;
59
    }
60
61
    /**
62
     * Callback to register an error on a field (Called from implementations of
63
     * {@link FormField::validate}). The optional error message type parameter is loaded into the
64
     * HTML class attribute.
65
     *
66
     * See {@link getErrors()} for details.
67
     *
68
     * @param string $fieldName Field name for this error
69
     * @param string $message The message string
70
     * @param string $messageType The type of message: e.g. "bad", "warning", "good", or "required". Passed as a CSS
71
     *                            class to the form, so other values can be used if desired.
72
     * @param string|bool $cast Cast type; One of the CAST_ constant definitions.
73
     * Bool values will be treated as plain text flag.
74
     * @return $this
75
     */
76
    public function validationError(
77
        $fieldName,
78
        $message,
79
        $messageType = ValidationResult::TYPE_ERROR,
80
        $cast = ValidationResult::CAST_TEXT
81
    ) {
82
        $this->result->addFieldError($fieldName, $message, $messageType, null, $cast);
83
        return $this;
84
    }
85
86
    /**
87
     * Returns all errors found by a previous call to {@link validate()}. The returned array has a
88
     * structure resembling:
89
     *
90
     * <code>
91
     *     array(
92
     *         'fieldName' => '[form field name]',
93
     *         'message' => '[validation error message]',
94
     *         'messageType' => '[bad|message|validation|required]',
95
     *         'messageCast' => '[text|html]'
96
     *     )
97
     * </code>
98
     *
99
     * @return null|array
100
     */
101
    public function getErrors()
102
    {
103
        if ($this->result) {
104
            return $this->result->getMessages();
105
        }
106
        return null;
107
    }
108
109
    /**
110
     * Get last validation result
111
     *
112
     * @return ValidationResult
113
     */
114
    public function getResult()
115
    {
116
        return $this->result;
117
    }
118
119
    /**
120
     * Returns whether the field in question is required. This will usually display '*' next to the
121
     * field. The base implementation always returns false.
122
     *
123
     * @param string $fieldName
124
     *
125
     * @return bool
126
     */
127
    public function fieldIsRequired($fieldName)
128
    {
129
        return false;
130
    }
131
132
    /**
133
     * @param array $data
134
     *
135
     * @return mixed
136
     */
137
    abstract public function php($data);
138
139
    /**
140
     * @param bool $enabled
141
     * @return $this
142
     */
143
    public function setEnabled($enabled)
144
    {
145
        $this->enabled = (bool)$enabled;
146
        return $this;
147
    }
148
149
    /**
150
     * @return bool
151
     */
152
    public function getEnabled()
153
    {
154
        return $this->enabled;
155
    }
156
157
    /**
158
     * @return $this
159
     */
160
    public function removeValidation()
161
    {
162
        $this->setEnabled(false);
163
        $this->resetResult();
164
        return $this;
165
    }
166
167
    /**
168
     * Clear current result
169
     *
170
     * @return $this
171
     */
172
    protected function resetResult()
173
    {
174
        $this->result = ValidationResult::create();
175
        return $this;
176
    }
177
}
178