Last active
November 3, 2018 17:39
-
-
Save marcaddeo/64023def3a69e3f444ee6a5375d7a0e3 to your computer and use it in GitHub Desktop.
Add error validation messages
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/password_policy.module b/password_policy.module | |
index 6b8d956..edd5c5b 100644 | |
--- a/password_policy.module | |
+++ b/password_policy.module | |
@@ -10,6 +10,7 @@ use Drupal\password_policy\Entity\PasswordPolicy; | |
use Drupal\Core\Entity\EntityInterface; | |
use Drupal\Core\Form\FormStateInterface; | |
use Drupal\user\UserInterface; | |
+use Drupal\Component\Render\FormattableMarkup; | |
/** | |
* Implements hook_form_FORM_ID_alter() for user_form(). | |
@@ -231,6 +232,7 @@ function _password_policy_constraints_validate(&$form, FormStateInterface &$form | |
$user_context_values[$user_context_field] = $form['account'][$user_context_field]['#default_value']; | |
} | |
} | |
+ $error_messages = [t('The password does not satisfy the password policies.')]; | |
/** @var \Drupal\password_policy\Entity\PasswordPolicy $policy */ | |
foreach ($applicable_policies as $policy_id => $policy) { | |
$policy_constraints = $policy->getConstraints(); | |
@@ -274,8 +276,9 @@ function _password_policy_constraints_validate(&$form, FormStateInterface &$form | |
$policies_table_rows[] = $table_row; | |
} | |
else { | |
- if (!$validation->isValid() and !$failed and $form_state->getValue('pass') != '') { | |
+ if (!$validation->isValid() and $form_state->getValue('pass') != '') { | |
// Throw error to ensure form will not submit. | |
+ $error_messages[] = $validation->getErrorMessage(); | |
$failed = TRUE; | |
} | |
elseif ($force_failure) { | |
@@ -285,7 +288,8 @@ function _password_policy_constraints_validate(&$form, FormStateInterface &$form | |
} | |
} | |
if ($failed && !$generate_policy_table) { | |
- $form_state->setErrorByName('pass', t('The password does not satisfy the password policies.')); | |
+ array_shift($error_messages); | |
+ $error_message = implode('<br/>', $error_messages); | |
+ $form_state->setErrorByName('pass', new FormattableMarkup($error_message, [])); | |
} | |
if ($generate_policy_table) { | |
return $policies_table_rows; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment