/** * {@inheritdoc} */ protected function flagViolations(EntityConstraintViolationListInterface $violations, 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. $field_names = array('label', 'machine_name'); foreach ($violations->getByFields($field_names) as $violation) { list($field_name) = explode('.', $violation->getPropertyPath(), 2); $form_state->setErrorByName($field_name, $violation->getMessage()); } parent::flagViolations($violations, $form, $form_state); }
/** * {@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); }
/** * {@inheritdoc} */ public function flagWidgetsErrorsFromViolations(EntityConstraintViolationListInterface $violations, array &$form, FormStateInterface $form_state) { $entity = $violations->getEntity(); foreach ($violations->getFieldNames() as $field_name) { // Only show violations for fields that actually appear in the form, and // let the widget assign the violations to the correct form elements. if ($widget = $this->getRenderer($field_name)) { $field_violations = $this->movePropertyPathViolationsRelativeToField($field_name, $violations->getByField($field_name)); $widget->flagErrors($entity->get($field_name), $field_violations, $form, $form_state); } } }
/** * Flags violations for the current 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 to array returned by * getEditedFieldNames() and overwrite this method in order to show any * violations for those fields; e.g.: * @code * foreach ($violations->getByField('name') as $violation) { * $form_state->setErrorByName('name', $violation->getMessage()); * } * parent::flagViolations($violations, $form, $form_state); * @endcode * * @param \Drupal\Core\Entity\EntityConstraintViolationListInterface $violations * The violations to flag. * @param array $form * A nested array of form elements comprising the form. * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. */ protected function flagViolations(EntityConstraintViolationListInterface $violations, array $form, FormStateInterface $form_state) { // Flag entity level violations. foreach ($violations->getEntityViolations() as $violation) { /** @var \Symfony\Component\Validator\ConstraintViolationInterface $violation */ $form_state->setErrorByName('', $violation->getMessage()); } // Let the form display flag violations of its fields. $this->getFormDisplay($form_state)->flagWidgetsErrorsFromViolations($violations, $form, $form_state); }