You are here

public function FieldableEdgeEntityForm::validateForm in Apigee Edge 8

TODO Add missing return type-hint in 2.x.

Overrides FormBase::validateForm

File

src/Entity/Form/FieldableEdgeEntityForm.php, line 70

Class

FieldableEdgeEntityForm
Base entity form for fieldable Apigee Edge entity types.

Namespace

Drupal\apigee_edge\Entity\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  parent::validateForm($form, $form_state);

  /** @var \Drupal\apigee_edge\Entity\FieldableEdgeEntityInterface $entity */
  $entity = $this
    ->buildEntity($form, $form_state);
  $violations = $entity
    ->validate();

  // Remove violations of inaccessible fields.
  $violations
    ->filterByFieldAccess($this
    ->currentUser());

  // In case a field-level submit button is clicked, for example the 'Add
  // another item' button for multi-value fields or the 'Upload' button for a
  // File or an Image field, make sure that we only keep violations for that
  // specific field.
  $edited_fields = [];
  if ($limit_validation_errors = $form_state
    ->getLimitValidationErrors()) {
    foreach ($limit_validation_errors as $section) {
      $field_name = reset($section);
      if ($entity
        ->hasField($field_name)) {
        $edited_fields[] = $field_name;
      }
    }
    $edited_fields = array_unique($edited_fields);
  }
  else {
    $edited_fields = $this
      ->getEditedFieldNames($form_state);
  }

  // Remove violations for fields that are not edited.
  $violations
    ->filterByFields(array_diff(array_keys($entity
    ->getFieldDefinitions()), $edited_fields));
  $this
    ->flagViolations($violations, $form, $form_state);

  // The entity was validated.
  $entity
    ->setValidationRequired(FALSE);
  $form_state
    ->setTemporaryValue('entity_validated', TRUE);
  return $entity;
}