2016-02-22 02:13:35 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Forms\Schema;
|
|
|
|
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Forms\CompositeField;
|
|
|
|
use SilverStripe\Forms\Form;
|
|
|
|
use SilverStripe\Forms\FormField;
|
2016-02-22 02:13:35 +01:00
|
|
|
|
2016-03-03 00:03:38 +01:00
|
|
|
/**
|
|
|
|
* Represents a {@link Form} as structured data which allows a frontend library to render it.
|
|
|
|
* Includes information about the form as well as its fields.
|
|
|
|
* Can create a "schema" (structure only) as well as "state" (data only).
|
|
|
|
*/
|
2016-02-22 02:13:35 +01:00
|
|
|
class FormSchema {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the schema for this form as a nested array.
|
|
|
|
*
|
|
|
|
* @param Form $form
|
2016-02-29 01:50:59 +01:00
|
|
|
* @return array
|
2016-02-22 02:13:35 +01:00
|
|
|
*/
|
|
|
|
public function getSchema(Form $form) {
|
2016-08-19 00:51:35 +02:00
|
|
|
$request = $form->getController()->getRequest();
|
2016-02-22 02:13:35 +01:00
|
|
|
|
|
|
|
$schema = [
|
|
|
|
'name' => $form->getName(),
|
2016-04-21 04:38:02 +02:00
|
|
|
'id' => $form->FormName(),
|
|
|
|
'action' => $form->FormAction(),
|
|
|
|
'method' => $form->FormMethod(),
|
|
|
|
// @todo Not really reliable. Refactor into action on $this->Link('schema')
|
2016-08-19 00:51:35 +02:00
|
|
|
'schema_url' => $request->getURL(),
|
2016-02-22 02:13:35 +01:00
|
|
|
'attributes' => $form->getAttributes(),
|
|
|
|
'data' => [],
|
|
|
|
'fields' => [],
|
|
|
|
'actions' => []
|
|
|
|
];
|
|
|
|
|
2016-09-07 05:35:47 +02:00
|
|
|
/** @var FormField $action */
|
2016-02-22 02:13:35 +01:00
|
|
|
foreach ($form->Actions() as $action) {
|
|
|
|
$schema['actions'][] = $action->getSchemaData();
|
|
|
|
}
|
|
|
|
|
2016-09-07 05:35:47 +02:00
|
|
|
/** @var FormField $field */
|
2016-07-06 06:34:09 +02:00
|
|
|
foreach ($form->Fields() as $field) {
|
2016-03-21 11:12:07 +01:00
|
|
|
$schema['fields'][] = $field->getSchemaData();
|
2016-02-22 02:13:35 +01:00
|
|
|
}
|
|
|
|
|
2016-02-29 01:50:59 +01:00
|
|
|
return $schema;
|
2016-02-22 02:13:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the current state of this form as a nested array.
|
|
|
|
*
|
2016-03-01 04:15:47 +01:00
|
|
|
* @param Form $form
|
2016-02-29 01:50:59 +01:00
|
|
|
* @return array
|
2016-02-22 02:13:35 +01:00
|
|
|
*/
|
|
|
|
public function getState(Form $form) {
|
2016-02-29 01:50:59 +01:00
|
|
|
$state = [
|
2016-04-21 04:38:02 +02:00
|
|
|
'id' => $form->FormName(),
|
2016-02-29 01:50:59 +01:00
|
|
|
'fields' => [],
|
|
|
|
'messages' => []
|
|
|
|
];
|
2016-02-22 02:13:35 +01:00
|
|
|
|
2016-08-12 06:34:51 +02:00
|
|
|
// flattened nested fields are returned, rather than only top level fields.
|
|
|
|
$state['fields'] = $this->getFieldStates($form->Fields());
|
2016-03-01 04:15:47 +01:00
|
|
|
|
|
|
|
if($form->Message()) {
|
|
|
|
$state['messages'][] = [
|
|
|
|
'value' => $form->Message(),
|
|
|
|
'type' => $form->MessageType(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-02-29 01:50:59 +01:00
|
|
|
return $state;
|
2016-02-22 02:13:35 +01:00
|
|
|
}
|
2016-08-12 06:34:51 +02:00
|
|
|
|
|
|
|
protected function getFieldStates($fields) {
|
|
|
|
$states = [];
|
|
|
|
foreach ($fields as $field) {
|
|
|
|
$states[] = $field->getSchemaState();
|
|
|
|
|
|
|
|
if ($field instanceof CompositeField) {
|
|
|
|
$subFields = $field->FieldList();
|
2016-09-07 05:35:47 +02:00
|
|
|
$states = array_merge($states, $this->getFieldStates($subFields));
|
2016-08-12 06:34:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $states;
|
|
|
|
}
|
2016-02-22 02:13:35 +01:00
|
|
|
}
|