|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package userforms |
|
5
|
|
|
*/ |
|
6
|
|
|
class UserForm extends Form { |
|
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* @param Controller $controller |
|
10
|
|
|
* @param string $name |
|
11
|
|
|
*/ |
|
12
|
|
|
public function __construct(Controller $controller, $name = 'Form') { |
|
13
|
|
|
|
|
14
|
|
|
$this->controller = $controller; |
|
15
|
|
|
$this->setRedirectToFormOnValidationError(true); |
|
16
|
|
|
|
|
17
|
|
|
parent::__construct( |
|
18
|
|
|
$controller, |
|
19
|
|
|
$name, |
|
20
|
|
|
new FieldList(), |
|
21
|
|
|
new FieldList() |
|
22
|
|
|
); |
|
23
|
|
|
|
|
24
|
|
|
$this->setFields($this->getFormFields()); |
|
25
|
|
|
$this->setActions($this->getFormActions()); |
|
26
|
|
|
$this->setValidator($this->getRequiredFields()); |
|
27
|
|
|
|
|
28
|
|
|
// This needs to be re-evaluated since fields have been assigned |
|
29
|
|
|
$this->setupFormErrors(); |
|
30
|
|
|
|
|
31
|
|
|
// Number each page |
|
32
|
|
|
$stepNumber = 1; |
|
33
|
|
|
foreach($this->getSteps() as $step) { |
|
34
|
|
|
$step->setStepNumber($stepNumber++); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
if($controller->DisableCsrfSecurityToken) { |
|
38
|
|
|
$this->disableSecurityToken(); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
$data = Session::get("FormInfo.{$this->FormName()}.data"); |
|
42
|
|
|
|
|
43
|
|
|
if(is_array($data)) { |
|
44
|
|
|
$this->loadDataFrom($data); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$this->extend('updateForm'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function setupFormErrors() |
|
51
|
|
|
{ |
|
52
|
|
|
// Suppress setupFormErrors if fields haven't been bootstrapped |
|
53
|
|
|
if($this->fields && $this->fields->exists()) { |
|
54
|
|
|
return parent::setupFormErrors(); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Used for partial caching in the template. |
|
62
|
|
|
* |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getLastEdited() { |
|
66
|
|
|
return $this->controller->LastEdited; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return bool |
|
71
|
|
|
*/ |
|
72
|
|
|
public function getDisplayErrorMessagesAtTop() { |
|
73
|
|
|
return (bool)$this->controller->DisplayErrorMessagesAtTop; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Return the fieldlist, filtered to only contain steps |
|
78
|
|
|
* |
|
79
|
|
|
* @return ArrayList |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getSteps() { |
|
82
|
|
|
return $this->Fields()->filterByCallback(function($field) { |
|
83
|
|
|
return $field instanceof UserFormsStepField; |
|
84
|
|
|
}); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Get the form fields for the form on this page. Can modify this FieldSet |
|
89
|
|
|
* by using {@link updateFormFields()} on an {@link Extension} subclass which |
|
90
|
|
|
* is applied to this controller. |
|
91
|
|
|
* |
|
92
|
|
|
* This will be a list of top level composite steps |
|
93
|
|
|
* |
|
94
|
|
|
* @return FieldList |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getFormFields() { |
|
97
|
|
|
$fields = new UserFormsFieldList(); |
|
98
|
|
|
$target = $fields; |
|
99
|
|
|
foreach ($this->controller->Fields() as $field) { |
|
100
|
|
|
$target = $target->processNext($field); |
|
101
|
|
|
} |
|
102
|
|
|
$fields->clearEmptySteps(); |
|
103
|
|
|
$this->extend('updateFormFields', $fields); |
|
104
|
|
|
$fields->setForm($this); |
|
105
|
|
|
return $fields; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Generate the form actions for the UserDefinedForm. You |
|
110
|
|
|
* can manipulate these by using {@link updateFormActions()} on |
|
111
|
|
|
* a decorator. |
|
112
|
|
|
* |
|
113
|
|
|
* @todo Make form actions editable via their own field editor. |
|
114
|
|
|
* |
|
115
|
|
|
* @return FieldList |
|
116
|
|
|
*/ |
|
117
|
|
|
public function getFormActions() { |
|
118
|
|
|
$submitText = ($this->controller->SubmitButtonText) ? $this->controller->SubmitButtonText : _t('UserDefinedForm.SUBMITBUTTON', 'Submit'); |
|
119
|
|
|
$clearText = ($this->controller->ClearButtonText) ? $this->controller->ClearButtonText : _t('UserDefinedForm.CLEARBUTTON', 'Clear'); |
|
120
|
|
|
|
|
121
|
|
|
$actions = new FieldList( |
|
122
|
|
|
new FormAction("process", $submitText) |
|
123
|
|
|
); |
|
124
|
|
|
|
|
125
|
|
|
if($this->controller->ShowClearButton) { |
|
126
|
|
|
$actions->push(new ResetFormAction("clearForm", $clearText)); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
$this->extend('updateFormActions', $actions); |
|
130
|
|
|
$actions->setForm($this); |
|
131
|
|
|
return $actions; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Get the required form fields for this form. |
|
136
|
|
|
* |
|
137
|
|
|
* @return RequiredFields |
|
138
|
|
|
*/ |
|
139
|
|
|
public function getRequiredFields() { |
|
140
|
|
|
// Generate required field validator |
|
141
|
|
|
$requiredNames = $this |
|
142
|
|
|
->getController() |
|
143
|
|
|
->Fields() |
|
144
|
|
|
->filter('Required', true) |
|
145
|
|
|
->column('Name'); |
|
146
|
|
|
$required = new RequiredFields($requiredNames); |
|
147
|
|
|
$this->extend('updateRequiredFields', $required); |
|
148
|
|
|
$required->setForm($this); |
|
149
|
|
|
return $required; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Override some we can add UserForm specific attributes to the form. |
|
154
|
|
|
* |
|
155
|
|
|
* @return array |
|
156
|
|
|
*/ |
|
157
|
|
|
public function getAttributes() { |
|
158
|
|
|
$attrs = parent::getAttributes(); |
|
159
|
|
|
|
|
160
|
|
|
$attrs['class'] = $attrs['class'] . ' userform'; |
|
161
|
|
|
$attrs['data-livevalidation'] = (bool)$this->controller->EnableLiveValidation; |
|
162
|
|
|
$attrs['data-toperrors'] = (bool)$this->controller->DisplayErrorMessagesAtTop; |
|
163
|
|
|
$attrs['data-hidefieldlabels'] = (bool)$this->controller->HideFieldLabels; |
|
164
|
|
|
|
|
165
|
|
|
return $attrs; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
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.