flagViolations example

// Manually flag violations of fields not handled by the form display. This     // is necessary as entity form displays only flag violations for fields     // contained in the display.     $field_names = [
      'label',
      'id',
    ];
    foreach ($violations->getByFields($field_names) as $violation) {
      [$field_name] = explode('.', $violation->getPropertyPath(), 2);
      $form_state->setErrorByName($field_name$violation->getMessage());
    }
    parent::flagViolations($violations$form$form_state);
  }

  /** * {@inheritdoc} */
  public function save(array $form, FormStateInterface $form_state) {
    $workspace = $this->entity;
    $workspace->setNewRevision(TRUE);
    $status = $workspace->save();

    $info = ['%info' => $workspace->label()];
    
/** * {@inheritdoc} */
  protected function flagViolations(EntityConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state) {
    // Manually flag violations of fields not handled by the form display.     foreach ($violations->getByField('created') as $violation) {
      $form_state->setErrorByName('date', $violation->getMessage());
    }
    foreach ($violations->getByField('name') as $violation) {
      $form_state->setErrorByName('name', $violation->getMessage());
    }
    parent::flagViolations($violations$form$form_state);
  }

  /** * Form submission handler for the 'preview' action. * * @param array $form * An associative array containing the structure of the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */
  public function preview(array &$form, FormStateInterface $form_state) {
    
// Manually flag violations of fields not handled by the form display. This     // is necessary as entity form displays only flag violations for fields     // contained in the display.     // @see ::form()     foreach ($violations->getByField('parent') as $violation) {
      $form_state->setErrorByName('parent', $violation->getMessage());
    }
    foreach ($violations->getByField('weight') as $violation) {
      $form_state->setErrorByName('weight', $violation->getMessage());
    }

    parent::flagViolations($violations$form$form_state);
  }

  /** * {@inheritdoc} */
  public function save(array $form, FormStateInterface $form_state) {
    $term = $this->entity;

    $result = $term->save();

    $edit_link = $term->toLink($this->t('Edit'), 'edit-form')->toString();
    

      }
      $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;
  }

  /** * Gets the names of all fields edited in the form. * * If the entity form customly adds some fields to the form (i.e. without * using the form display), it needs to add its fields here and override * flagViolations() for displaying the violations. * * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. * * @return string[] * An array of field names. */
'pass',
      'mail',
      'timezone',
      'langcode',
      'preferred_langcode',
      'preferred_admin_langcode',
    ];
    foreach ($violations->getByFields($field_names) as $violation) {
      [$field_name] = explode('.', $violation->getPropertyPath(), 2);
      $form_state->setErrorByName($field_name$violation->getMessage());
    }
    parent::flagViolations($violations$form$form_state);
  }

  /** * {@inheritdoc} */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form$form_state);

    $user = $this->getEntity();
    // If there's a session set to the users id, remove the password reset tag     // since a new password was saved.
Home | Imprint | This part of the site doesn't use cookies.