1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
class UserFormValidator extends RequiredFields |
|
|
|
|
5
|
|
|
{ |
6
|
1 |
|
public function php($data) |
7
|
|
|
{ |
8
|
1 |
|
if (!parent::php($data)) { |
9
|
|
|
return false; |
10
|
|
|
} |
11
|
|
|
|
12
|
|
|
// Skip unsaved records |
13
|
1 |
|
if (empty($data['ID']) || !is_numeric($data['ID'])) { |
14
|
|
|
return true; |
15
|
|
|
} |
16
|
|
|
|
17
|
1 |
|
$fields = EditableFormField::get()->filter('ParentID', $data['ID'])->sort('"Sort" ASC'); |
18
|
|
|
|
19
|
|
|
// Current nesting |
20
|
1 |
|
$stack = array(); |
21
|
1 |
|
$conditionalStep = false; // Is the current step conditional? |
22
|
1 |
|
foreach ($fields as $field) { |
23
|
1 |
|
if ($field instanceof EditableFormStep) { |
24
|
|
|
// Page at top level, or after another page is ok |
25
|
1 |
|
if (empty($stack) || (count($stack) === 1 && $stack[0] instanceof EditableFormStep)) { |
26
|
1 |
|
$stack = array($field); |
27
|
1 |
|
$conditionalStep = $field->EffectiveDisplayRules()->count() > 0; |
28
|
1 |
|
continue; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$this->validationError( |
32
|
|
|
'FormFields', |
33
|
|
|
_t( |
34
|
|
|
"UserFormValidator.UNEXPECTED_BREAK", |
35
|
|
|
"Unexpected page break '{name}' inside nested field '{group}'", |
36
|
|
|
array( |
|
|
|
|
37
|
|
|
'name' => $field->CMSTitle, |
|
|
|
|
38
|
|
|
'group' => end($stack)->CMSTitle |
39
|
|
|
) |
40
|
|
|
), |
41
|
|
|
'error' |
42
|
|
|
); |
43
|
|
|
return false; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// Validate no pages |
47
|
1 |
|
if (empty($stack)) { |
48
|
|
|
$this->validationError( |
49
|
|
|
'FormFields', |
50
|
|
|
_t( |
51
|
|
|
"UserFormValidator.NO_PAGE", |
52
|
|
|
"Field '{name}' found before any pages", |
53
|
|
|
array( |
|
|
|
|
54
|
|
|
'name' => $field->CMSTitle |
55
|
|
|
) |
56
|
|
|
), |
57
|
|
|
'error' |
58
|
|
|
); |
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
// Nest field group |
63
|
1 |
|
if ($field instanceof EditableFieldGroup) { |
64
|
1 |
|
$stack[] = $field; |
65
|
1 |
|
continue; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Unnest field group |
69
|
1 |
|
if ($field instanceof EditableFieldGroupEnd) { |
70
|
1 |
|
$top = end($stack); |
71
|
|
|
|
72
|
|
|
// Check that the top is a group at all |
73
|
1 |
|
if (!$top instanceof EditableFieldGroup) { |
74
|
|
|
$this->validationError( |
75
|
|
|
'FormFields', |
76
|
|
|
_t( |
77
|
|
|
"UserFormValidator.UNEXPECTED_GROUP_END", |
78
|
|
|
"'{name}' found without a matching group", |
79
|
|
|
array( |
|
|
|
|
80
|
|
|
'name' => $field->CMSTitle |
|
|
|
|
81
|
|
|
) |
82
|
|
|
), |
83
|
|
|
'error' |
84
|
|
|
); |
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// Check that the top is the right group |
89
|
1 |
View Code Duplication |
if ($top->EndID != $field->ID) { |
|
|
|
|
90
|
|
|
$this->validationError( |
91
|
|
|
'FormFields', |
92
|
|
|
_t( |
93
|
|
|
"UserFormValidator.WRONG_GROUP_END", |
94
|
|
|
"'{name}' found closes the wrong group '{group}'", |
95
|
|
|
array( |
|
|
|
|
96
|
|
|
'name' => $field->CMSTitle, |
|
|
|
|
97
|
|
|
'group' => $top->CMSTitle |
|
|
|
|
98
|
|
|
) |
99
|
|
|
), |
100
|
|
|
'error' |
101
|
|
|
); |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
// Unnest group |
106
|
1 |
|
array_pop($stack); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// Normal field type |
110
|
1 |
View Code Duplication |
if ($conditionalStep && $field->Required) { |
|
|
|
|
111
|
|
|
$this->validationError( |
112
|
|
|
'FormFields', |
113
|
|
|
_t( |
114
|
|
|
"UserFormValidator.CONDITIONAL_REQUIRED", |
115
|
|
|
"Required field '{name}' cannot be placed within a conditional page", |
116
|
|
|
array( |
|
|
|
|
117
|
|
|
'name' => $field->CMSTitle |
118
|
|
|
) |
119
|
|
|
), |
120
|
|
|
'error' |
121
|
|
|
); |
122
|
1 |
|
return false; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
return true; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.