mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Initial schemaState implementation
This commit is contained in:
parent
afccef718c
commit
404ac4ae43
@ -182,7 +182,43 @@ class LeftAndMain extends Controller implements PermissionProvider {
|
||||
* @return SS_HTTPResponse
|
||||
*/
|
||||
public function schema() {
|
||||
return $this->schema->getSchema($this->getEditForm());
|
||||
$req = $this->getRequest();
|
||||
$res = $this->getResponse();
|
||||
$schemaParts = [];
|
||||
|
||||
// Valid values for the "X-Formschema-Request" header are "schema" and "state".
|
||||
// If either of these values are set they will be stored in the $schemaParst array
|
||||
// and used to construct the response body.
|
||||
if ($schemaHeader = $req->getHeader('X-Formschema-Request')) {
|
||||
$schemaParts = array_filter(explode(',', $schemaHeader), function($value) {
|
||||
$validHeaderValues = ['schema', 'state'];
|
||||
return in_array(trim($value), $validHeaderValues);
|
||||
});
|
||||
}
|
||||
|
||||
// Make sure it's an AJAX GET request with a valid "X-Formschema-Request" header value.
|
||||
if (!$req->isAjax() || !$req->isGET() || !count($schemaParts)) {
|
||||
throw new SS_HTTPResponse_Exception(
|
||||
'Invalid request. Check you\'ve set a "X-Formschema-Request" header with "schema" or "state" values.',
|
||||
400
|
||||
);
|
||||
}
|
||||
|
||||
$form = $this->getEditForm();
|
||||
$responseBody = ['id' => $form->getName()];
|
||||
|
||||
if (in_array('schema', $schemaParts)) {
|
||||
$responseBody['schema'] = $this->schema->getSchema($form);
|
||||
}
|
||||
|
||||
if (in_array('state', $schemaParts)) {
|
||||
$responseBody['state'] = $this->schema->getState($form);
|
||||
}
|
||||
|
||||
$res->addHeader('Content-Type', 'application/json');
|
||||
$res->setBody(Convert::raw2json($responseBody));
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -11,7 +11,7 @@ class FormSchema {
|
||||
* Gets the schema for this form as a nested array.
|
||||
*
|
||||
* @param Form $form
|
||||
* @return string
|
||||
* @return array
|
||||
*/
|
||||
public function getSchema(Form $form) {
|
||||
$request = $form->controller()->getRequest();
|
||||
@ -39,18 +39,22 @@ class FormSchema {
|
||||
}
|
||||
}
|
||||
|
||||
return Convert::raw2json($schema);
|
||||
return $schema;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current state of this form as a nested array.
|
||||
*
|
||||
* @param From $form
|
||||
* @return string
|
||||
* @return array
|
||||
*/
|
||||
public function getState(Form $form) {
|
||||
$state = ['state' => []];
|
||||
$state = [
|
||||
'id' => $form->getName(),
|
||||
'fields' => [],
|
||||
'messages' => []
|
||||
];
|
||||
|
||||
return Convert::raw2json($state);
|
||||
return $state;
|
||||
}
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ class FormSchemaTest extends SapphireTest {
|
||||
public function testGetSchema() {
|
||||
$form = new Form(new Controller(), 'TestForm', new FieldList(), new FieldList());
|
||||
$formSchema = new FormSchema();
|
||||
$expectedJSON = json_encode([
|
||||
$expected = [
|
||||
'name' => 'TestForm',
|
||||
'id' => null,
|
||||
'action' => null,
|
||||
@ -43,20 +43,24 @@ class FormSchemaTest extends SapphireTest {
|
||||
],
|
||||
],
|
||||
'actions' => []
|
||||
]);
|
||||
];
|
||||
|
||||
$schema = $formSchema->getSchema($form);
|
||||
|
||||
$this->assertJsonStringEqualsJsonString($expectedJSON, $schema);
|
||||
$this->assertInternalType('array', $schema);
|
||||
$this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($schema));
|
||||
}
|
||||
|
||||
public function testGetState() {
|
||||
$form = new Form(new Controller(), 'TestForm', new FieldList(), new FieldList());
|
||||
$formSchema = new FormSchema();
|
||||
$expectedJSON = json_encode(['state' => []]);
|
||||
$expected = [
|
||||
'id' => 'TestForm',
|
||||
'fields' => [],
|
||||
'messages' => []
|
||||
];
|
||||
|
||||
$state = $formSchema->getState($form);
|
||||
|
||||
$this->assertJsonStringEqualsJsonString($expectedJSON, $state);
|
||||
$this->assertInternalType('array', $state);
|
||||
$this->assertJsonStringEqualsJsonString(json_encode($expected), json_encode($state));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user