|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms\Schema; |
|
4
|
|
|
|
|
5
|
|
|
use Form; |
|
6
|
|
|
use FormField; |
|
7
|
|
|
use CompositeField; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class FormSchema |
|
11
|
|
|
* @package SilverStripe\Forms\Schema |
|
12
|
|
|
* |
|
13
|
|
|
* Represents a {@link Form} as structured data which allows a frontend library to render it. |
|
14
|
|
|
* Includes information about the form as well as its fields. |
|
15
|
|
|
* Can create a "schema" (structure only) as well as "state" (data only). |
|
16
|
|
|
*/ |
|
17
|
|
|
class FormSchema { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Gets the schema for this form as a nested array. |
|
21
|
|
|
* |
|
22
|
|
|
* @param Form $form |
|
23
|
|
|
* @return array |
|
24
|
|
|
*/ |
|
25
|
|
|
public function getSchema(Form $form) { |
|
26
|
|
|
$request = $form->controller()->getRequest(); |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
$schema = [ |
|
29
|
|
|
'name' => $form->getName(), |
|
30
|
|
|
'id' => $form->FormName(), |
|
31
|
|
|
'action' => $form->FormAction(), |
|
32
|
|
|
'method' => $form->FormMethod(), |
|
33
|
|
|
// @todo Not really reliable. Refactor into action on $this->Link('schema') |
|
34
|
|
|
'schema_url' => $request->getUrl(), |
|
35
|
|
|
'attributes' => $form->getAttributes(), |
|
36
|
|
|
'data' => [], |
|
37
|
|
|
'fields' => [], |
|
38
|
|
|
'actions' => [] |
|
39
|
|
|
]; |
|
40
|
|
|
|
|
41
|
|
|
foreach ($form->Actions() as $action) { |
|
|
|
|
|
|
42
|
|
|
/** @var FormField $action */ |
|
43
|
|
|
$schema['actions'][] = $action->getSchemaData(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
foreach ($form->Fields() as $field) { |
|
|
|
|
|
|
47
|
|
|
/** @var FormField $field */ |
|
48
|
|
|
$schema['fields'][] = $field->getSchemaData(); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $schema; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Gets the current state of this form as a nested array. |
|
56
|
|
|
* |
|
57
|
|
|
* @param Form $form |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getState(Form $form) { |
|
61
|
|
|
$state = [ |
|
62
|
|
|
'id' => $form->FormName(), |
|
63
|
|
|
'fields' => [], |
|
64
|
|
|
'messages' => [] |
|
65
|
|
|
]; |
|
66
|
|
|
|
|
67
|
|
|
// flattened nested fields are returned, rather than only top level fields. |
|
68
|
|
|
$state['fields'] = $this->getFieldStates($form->Fields()); |
|
69
|
|
|
|
|
70
|
|
|
if($form->Message()) { |
|
|
|
|
|
|
71
|
|
|
$state['messages'][] = [ |
|
72
|
|
|
'value' => $form->Message(), |
|
73
|
|
|
'type' => $form->MessageType(), |
|
74
|
|
|
]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return $state; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
protected function getFieldStates($fields) { |
|
81
|
|
|
$states = []; |
|
82
|
|
|
foreach ($fields as $field) { |
|
83
|
|
|
$states[] = $field->getSchemaState(); |
|
84
|
|
|
|
|
85
|
|
|
if ($field instanceof CompositeField) { |
|
86
|
|
|
$subFields = $field->FieldList(); |
|
87
|
|
|
array_merge($states, $this->getFieldStates($subFields)); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
return $states; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.