/**
  * Gets the current state of this form as a nested array.
  *
  * @param Form $form
  * @return array
  */
 public function getState(Form $form)
 {
     // Ensure that session errors are populated within form field messages
     $form->setupFormErrors();
     // @todo - Replace with ValidationResult handling
     // Currently tri-state; null (unsubmitted), true (submitted-valid), false (submitted-invalid)
     $errors = Session::get("FormInfo.{$form->FormName()}.errors");
     $valid = isset($errors) ? empty($errors) : null;
     $state = ['id' => $form->FormName(), 'fields' => [], 'valid' => $valid, 'messages' => []];
     // flattened nested fields are returned, rather than only top level fields.
     $state['fields'] = array_merge($this->getFieldStates($form->Fields()), $this->getFieldStates($form->Actions()));
     if ($message = $form->Message()) {
         $state['messages'][] = ['value' => ['html' => $message], 'type' => $form->MessageType()];
     }
     return $state;
 }
 public function testGetSchemaStateWithFormValidation()
 {
     $field = new FormField('MyField', 'My Field');
     $validator = new RequiredFields('MyField');
     $form = new Form(new Controller(), 'TestForm', new FieldList($field), new FieldList(), $validator);
     $form->validate();
     $form->setupFormErrors();
     $schema = $field->getSchemaState();
     $this->assertEquals(['html' => '"My Field" is required'], $schema['message']['value']);
 }