1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\Forms\Schema; |
4
|
|
|
|
5
|
|
|
use Convert; |
6
|
|
|
use Form; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class FormSchema |
10
|
|
|
* @package SilverStripe\Forms\Schema |
11
|
|
|
* |
12
|
|
|
* Represents a {@link Form} as structured data which allows a frontend library to render it. |
13
|
|
|
* Includes information about the form as well as its fields. |
14
|
|
|
* Can create a "schema" (structure only) as well as "state" (data only). |
15
|
|
|
*/ |
16
|
|
|
class FormSchema { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Gets the schema for this form as a nested array. |
20
|
|
|
* |
21
|
|
|
* @param Form $form |
22
|
|
|
* @return array |
23
|
|
|
*/ |
24
|
|
|
public function getSchema(Form $form) { |
25
|
|
|
$request = $form->controller()->getRequest(); |
|
|
|
|
26
|
|
|
$params = $request->AllParams(); |
27
|
|
|
|
28
|
|
|
$schema = [ |
29
|
|
|
'name' => $form->getName(), |
30
|
|
|
'id' => isset($params['ID']) ? $params['ID'] : null, |
31
|
|
|
'action' => isset($params['Action']) ? $params['Action'] : null, |
32
|
|
|
'method' => $form->controller()->getRequest()->HttpMethod(), |
|
|
|
|
33
|
|
|
'schema_url' => $request->getUrl(), |
34
|
|
|
'attributes' => $form->getAttributes(), |
35
|
|
|
'data' => [], |
36
|
|
|
'fields' => [], |
37
|
|
|
'actions' => [] |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
foreach ($form->Actions() as $action) { |
|
|
|
|
41
|
|
|
$schema['actions'][] = $action->getSchemaData(); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
foreach ($form->Fields() as $fieldList) { |
|
|
|
|
45
|
|
|
foreach ($fieldList->getForm()->fields()->dataFields() as $field) { |
46
|
|
|
$schema['fields'][] = $field->getSchemaData(); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
return $schema; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Gets the current state of this form as a nested array. |
55
|
|
|
* |
56
|
|
|
* @param Form $form |
57
|
|
|
* @return array |
58
|
|
|
*/ |
59
|
|
|
public function getState(Form $form) { |
60
|
|
|
$state = [ |
61
|
|
|
'id' => $form->getName(), |
62
|
|
|
'fields' => [], |
63
|
|
|
'messages' => [] |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
foreach ($form->Fields()->dataFields() as $field) { |
67
|
|
|
$state['fields'][] = $field->getSchemaState(); |
68
|
|
|
} |
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
|
|
|
|
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.