You are here

public function FormWidgetExampleForm::validateForm in Typed Data API enhancements 8

Form validation handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormBase::validateForm

File

tests/modules/typed_data_widget_test/src/FormWidgetExampleForm.php, line 142

Class

FormWidgetExampleForm
Class FormWidgetExampleForm.

Namespace

Drupal\typed_data_widget_test

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);
  $context_definition = $form_state
    ->get('context_definition');
  $widget_id = $form_state
    ->get('widget_id');
  $widget = $this
    ->getFormWidgetManager()
    ->createInstance($widget_id);
  $subform_state = SubformState::createWithParents([
    'data',
  ], $form, $form_state);
  $data = $this
    ->getTypedDataManager()
    ->create($context_definition
    ->getDataDefinition());
  $widget
    ->extractFormValues($data, $subform_state);

  // Validate the data and flag possible violations.
  $violations = $data
    ->validate();
  $widget
    ->flagViolations($data, $violations, $subform_state);
}