-
Notifications
You must be signed in to change notification settings - Fork 823
/
Copy pathRequiredFields.php
277 lines (242 loc) · 7.51 KB
/
RequiredFields.php
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
<?php
namespace SilverStripe\Forms;
use SilverStripe\ORM\ArrayLib;
use SilverStripe\Dev\Deprecation;
/**
* Required Fields allows you to set which fields need to be present before
* submitting the form. Submit an array of arguments or each field as a separate
* argument.
*
* Validation is performed on a field by field basis through
* {@link FormField::validate}.
*
* @deprecated 5.4.0 Will be renamed to SilverStripe\Forms\Validation\RequiredFieldsValidator
*/
class RequiredFields extends Validator
{
/**
* Whether to globally allow whitespace only as a valid value for a required field
* Can be overridden on a per-instance basis
*/
private static bool $allow_whitespace_only = true;
/**
* List of required fields
*
* @var array
*/
protected $required;
/**
* Whether to allow whitespace only as a valid value for a required field for this instance
* By default, this is set to null which will revert to the global default
*/
private ?bool $allowWhitespaceOnly = null;
/**
* Pass each field to be validated as a separate argument to the constructor
* of this object. (an array of elements are ok).
*/
public function __construct()
{
Deprecation::noticeWithNoReplacment(
'5.4.0',
'Will be renamed to SilverStripe\\Forms\\Validation\\RequiredFieldsValidator',
Deprecation::SCOPE_CLASS
);
$required = func_get_args();
if (isset($required[0]) && is_array($required[0])) {
$required = $required[0];
}
if (!empty($required)) {
$this->required = ArrayLib::valuekey($required);
} else {
$this->required = [];
}
parent::__construct();
}
/**
* Get whether to allow whitespace only as a valid value for a required field
*/
public function getAllowWhitespaceOnly(): ?bool
{
return $this->allowWhitespaceOnly ?? static::config()->get('allow_whitespace_only');
}
/**
* Set whether to allow whitespace only as a valid value for a required field
*/
public function setAllowWhitespaceOnly(?bool $allow)
{
$this->allowWhitespaceOnly = $allow;
}
/**
* Clears all the validation from this object.
*
* @return $this
*/
public function removeValidation()
{
parent::removeValidation();
$this->required = [];
return $this;
}
/**
* Debug helper
* @return string
*/
public function debug()
{
if (!is_array($this->required)) {
return false;
}
$result = "<ul>";
foreach ($this->required as $name) {
$result .= "<li>$name</li>";
}
$result .= "</ul>";
return $result;
}
/**
* Allows validation of fields via specification of a php function for
* validation which is executed after the form is submitted.
*
* @param array $data
*
* @return boolean
*/
public function php($data)
{
$valid = true;
$fields = $this->form->Fields();
foreach ($fields as $field) {
$valid = ($field->validate($this) && $valid);
}
if (!$this->required) {
return $valid;
}
foreach ($this->required as $fieldName) {
if (!$fieldName) {
continue;
}
if ($fieldName instanceof FormField) {
$formField = $fieldName;
$fieldName = $fieldName->getName();
} else {
$formField = $fields->dataFieldByName($fieldName);
}
// submitted data for file upload fields come back as an array
$value = isset($data[$fieldName]) ? $data[$fieldName] : null;
if (is_array($value)) {
if ($formField instanceof FileField && isset($value['error']) && $value['error']) {
$error = true;
} else {
if (is_a($formField, HasOneRelationFieldInterface::class) && isset($value['value'])) {
$stringValue = (string) $value['value'];
$error = in_array($stringValue, ['0', '']);
} else {
$error = (count($value ?? [])) ? false : true;
}
}
} else {
$stringValue = (string) $value;
if (!$this->getAllowWhitespaceOnly()) {
$stringValue = preg_replace('/^\s+/u', '', $stringValue);
$stringValue = preg_replace('/\s+$/u', '', $stringValue);
}
if (is_a($formField, HasOneRelationFieldInterface::class)) {
// test for blank string as well as '0' because older versions of silverstripe/admin FormBuilder
// forms created using redux-form would have a value of null for unsaved records
// the null value will have been converted to '' by the time it gets to this point
$error = in_array($stringValue, ['0', '']);
} else {
$error = strlen($stringValue) > 0 ? false : true;
}
}
if ($formField && $error) {
$errorMessage = _t(
'SilverStripe\\Forms\\Form.FIELDISREQUIRED',
'{name} is required',
[
'name' => strip_tags(
'"' . ($formField->Title() ? $formField->Title() : $fieldName) . '"'
)
]
);
if ($msg = $formField->getCustomValidationMessage()) {
$errorMessage = $msg;
}
$this->validationError(
$fieldName,
$errorMessage,
"required"
);
$valid = false;
}
}
return $valid;
}
/**
* Adds a single required field to required fields stack.
*
* @param string $field
*
* @return $this
*/
public function addRequiredField($field)
{
$this->required[$field] = $field;
return $this;
}
/**
* Removes a required field
*
* @param string $field
*
* @return $this
*/
public function removeRequiredField($field)
{
unset($this->required[$field]);
return $this;
}
/**
* Add {@link RequiredField} objects together
*
* @param RequiredFields $requiredFields
* @return $this
*/
public function appendRequiredFields($requiredFields)
{
$this->required = $this->required + ArrayLib::valuekey(
$requiredFields->getRequired()
);
return $this;
}
/**
* Returns true if the named field is "required".
*
* Used by {@link FormField} to return a value for FormField::Required(),
* to do things like show *s on the form template.
*
* @param string $fieldName
*
* @return boolean
*/
public function fieldIsRequired($fieldName)
{
return isset($this->required[$fieldName]);
}
/**
* Return the required fields
*
* @return array
*/
public function getRequired()
{
return array_values($this->required ?? []);
}
/**
* @return bool
*/
public function canBeCached(): bool
{
return count($this->getRequired() ?? []) === 0;
}
}