public static function WebformEmailConfirm::validateWebformEmailConfirm in Webform 8.5
Same name and namespace in other branches
- 6.x src/Element/WebformEmailConfirm.php \Drupal\webform\Element\WebformEmailConfirm::validateWebformEmailConfirm()
Validates an email confirm element.
File
- src/
Element/ WebformEmailConfirm.php, line 168
Class
- WebformEmailConfirm
- Provides a webform element requiring users to double-element and confirm an email address.
Namespace
Drupal\webform\ElementCode
public static function validateWebformEmailConfirm(&$element, FormStateInterface $form_state, &$complete_form) {
if (isset($element['flexbox'])) {
$mail_element =& $element['flexbox'];
}
else {
$mail_element =& $element;
}
$mail_1 = trim($mail_element['mail_1']['#value']);
$mail_2 = trim($mail_element['mail_2']['#value']);
if (Element::isVisibleElement($element)) {
// Compare email addresses.
if ((!empty($mail_1) || !empty($mail_2)) && strcmp($mail_1, $mail_2)) {
$form_state
->setError($element, t('The specified email addresses do not match.'));
}
else {
// NOTE: Only mail_1 needs to be validated since mail_2 is the same value.
// Verify the required value.
if ($mail_element['mail_1']['#required'] && empty($mail_1)) {
$required_error_title = isset($mail_element['mail_1']['#title']) ? $mail_element['mail_1']['#title'] : NULL;
WebformElementHelper::setRequiredError($element, $form_state, $required_error_title);
}
// Verify that the value is not longer than #maxlength.
if (isset($mail_element['mail_1']['#maxlength']) && mb_strlen($mail_1) > $mail_element['mail_1']['#maxlength']) {
$t_args = [
'@name' => $mail_element['mail_1']['#title'],
'%max' => $mail_element['mail_1']['#maxlength'],
'%length' => mb_strlen($mail_1),
];
$form_state
->setError($element, t('@name cannot be longer than %max characters but is currently %length characters long.', $t_args));
}
}
// Add email validation errors for inline form errors.
// @see \Drupal\Core\Render\Element\Email::validateEmail
$inline_errors = empty($complete_form['#disable_inline_form_errors']) && \Drupal::moduleHandler()
->moduleExists('inline_form_errors');
$mail_error = $form_state
->getError($mail_element['mail_1']);
if ($inline_errors && $mail_error) {
$form_state
->setError($element, $mail_error);
}
}
// Set #title for other validation callbacks.
// @see \Drupal\webform\Plugin\WebformElementBase::validateUnique
if (isset($mail_element['mail_1']['#title'])) {
$element['#title'] = $mail_element['mail_1']['#title'];
}
// Email field must be converted from a two-element array into a single
// string regardless of validation results.
$form_state
->setValueForElement($mail_element['mail_1'], NULL);
$form_state
->setValueForElement($mail_element['mail_2'], NULL);
$element['#value'] = $mail_1;
$form_state
->setValueForElement($element, $mail_1);
}