1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class ConsoleForm |
5
|
|
|
*/ |
6
|
|
|
class ConsoleForm extends Form |
|
|
|
|
7
|
|
|
{ |
8
|
|
|
/** |
9
|
|
|
* @var ContentController |
10
|
|
|
*/ |
11
|
|
|
protected $controller; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @param Controller $controller |
15
|
|
|
* @param string $name |
16
|
|
|
*/ |
17
|
|
|
public function __construct(Controller $controller, $name) |
18
|
|
|
{ |
19
|
|
|
$this->controller = $controller; |
|
|
|
|
20
|
|
|
|
21
|
|
|
$fields = $this->getCustomFields(); |
22
|
|
|
$actions = $this->getCustomActions(); |
23
|
|
|
$validator = $this->getCustomValidator($fields); |
24
|
|
|
|
25
|
|
|
parent::__construct($controller, $name, $fields, $actions, $validator); |
26
|
|
|
|
27
|
|
|
$this->setFormAction('/console/'.$name); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array $data |
32
|
|
|
* @param ConsoleForm $form |
33
|
|
|
* @param SS_HTTPRequest $request |
34
|
|
|
*/ |
35
|
|
|
public function submitForm($data, ConsoleForm $form, SS_HTTPRequest $request) |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
//TODO : do something with data or form |
38
|
|
|
|
39
|
|
|
return $this->controller->redirect('/console/index/'.$data['Name']); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return FieldList |
44
|
|
|
*/ |
45
|
|
|
public function getCustomFields() |
46
|
|
|
{ |
47
|
|
|
$fields = new FieldList( |
48
|
|
|
new TextField('Name', 'Name') |
49
|
|
|
); |
50
|
|
|
|
51
|
|
|
return $fields; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @return FieldList |
56
|
|
|
*/ |
57
|
|
|
public function getCustomActions() |
58
|
|
|
{ |
59
|
|
|
$fields = new FieldList( |
60
|
|
|
new FormAction('submitForm', _t('Site.Submit', 'Submit')) |
61
|
|
|
); |
62
|
|
|
|
63
|
|
|
return $fields; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function getCustomValidator(FieldList $fields) |
67
|
|
|
{ |
68
|
|
|
$required = []; |
69
|
|
|
|
70
|
|
|
$this->setRequiredFieldsLabels($required, $fields); |
71
|
|
|
|
72
|
|
|
return RequiredFields::create($required); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Add required stars to field labels |
77
|
|
|
* |
78
|
|
|
* @param array $required |
79
|
|
|
* @param FieldList $fields |
80
|
|
|
*/ |
81
|
|
|
protected function setRequiredFieldsLabels($required, FieldList $fields) |
82
|
|
|
{ |
83
|
|
|
foreach ((array)$required as $req) { |
84
|
|
|
$field = $fields->dataFieldByName($req); |
85
|
|
|
if ($field && $title = trim($field->Title())) { |
86
|
|
|
if (!$field->getCustomValidationMessage()) { |
87
|
|
|
$message = strip_tags($title) . ' ' . _t('Site.IsRequired', ' is required'); |
88
|
|
|
$field->setCustomValidationMessage($message); |
89
|
|
|
} |
90
|
|
|
$field->setTitle($title . ' *'); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
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.