function AccountForm::flagViolations
Same name and namespace in other branches
- 9 core/modules/user/src/AccountForm.php \Drupal\user\AccountForm::flagViolations()
- 8.9.x core/modules/user/src/AccountForm.php \Drupal\user\AccountForm::flagViolations()
- 11.x core/modules/user/src/AccountForm.php \Drupal\user\AccountForm::flagViolations()
Flags violations for the current form.
If a custom entity form 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.:
foreach ($violations->getByField('name') as $violation) {
$form_state->setErrorByName('name', $violation->getMessage());
}
parent::flagViolations($violations, $form, $form_state);
Parameters
\Drupal\Core\Entity\EntityConstraintViolationListInterface $violations: The violations to flag.
array $form: A nested array of form elements comprising the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Overrides ContentEntityForm::flagViolations
File
-
core/
modules/ user/ src/ AccountForm.php, line 413
Class
- AccountForm
- Form controller for the user account forms.
Namespace
Drupal\userCode
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 = [
'name',
'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);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.