silverstripe-framework/forms/FormSchema.php

72 lines
1.5 KiB
PHP
Raw Normal View History

<?php
namespace SilverStripe\Forms\Schema;
use Convert;
use Form;
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
*/
public function getSchema(Form $form) {
$request = $form->controller()->getRequest();
$params = $request->AllParams();
$schema = [
'name' => $form->getName(),
'id' => isset($params['ID']) ? $params['ID'] : null,
'action' => isset($params['Action']) ? $params['Action'] : null,
'method' => $form->controller()->getRequest()->HttpMethod(),
'schema_url' => $request->getUrl(),
'attributes' => $form->getAttributes(),
'data' => [],
'fields' => [],
'actions' => []
];
foreach ($form->Actions() as $action) {
$schema['actions'][] = $action->getSchemaData();
}
foreach ($form->Fields() as $fieldList) {
foreach ($fieldList->getForm()->fields()->dataFields() as $field) {
$schema['fields'][] = $field->getSchemaData();
}
}
2016-02-29 01:50:59 +01:00
return $schema;
}
/**
* Gets the current state of this form as a nested array.
*
* @param Form $form
2016-02-29 01:50:59 +01:00
* @return array
*/
public function getState(Form $form) {
2016-02-29 01:50:59 +01:00
$state = [
'id' => $form->getName(),
'fields' => [],
'messages' => []
];
foreach ($form->Fields()->dataFields() as $field) {
$state['fields'][] = $field->getSchemaState();
}
if($form->Message()) {
$state['messages'][] = [
'value' => $form->Message(),
'type' => $form->MessageType(),
];
}
2016-02-29 01:50:59 +01:00
return $state;
}
}