public function MobileNumber::mobileNumberValidate in Mobile Number 8
Same name and namespace in other branches
- 2.0.x src/Element/MobileNumber.php \Drupal\mobile_number\Element\MobileNumber::mobileNumberValidate()
Mobile number element validate callback.
Parameters
array $element: Element.
\Drupal\Core\Form\FormStateInterface $form_state: Form state.
array $complete_form: Complete form.
Return value
array Element.
File
- src/
Element/ MobileNumber.php, line 290
Class
- MobileNumber
- Provides a form input element for entering an email address.
Namespace
Drupal\mobile_number\ElementCode
public function mobileNumberValidate($element, FormStateInterface $form_state, &$complete_form) {
/** @var \Drupal\mobile_number\MobileNumberUtilInterface $util */
$util = \Drupal::service('mobile_number.util');
$settings = $element['#mobile_number'];
$op = $this
->getOp($element, $form_state);
$field_label = !empty($element['#field_title']) ? $element['#field_title'] : $element['#title'];
$tree_parents = $element['#parents'];
$field_path = implode('][', $tree_parents);
$input = NestedArray::getValue($form_state
->getUserInput(), $tree_parents);
$input = $input ? $input : [];
$mobile_number = NULL;
$countries = $util
->getCountryOptions([], TRUE);
$token = !empty($element['#value']['verification_token']) ? $element['#value']['verification_token'] : FALSE;
if ($input) {
$input += count($settings['allowed_countries']) == 1 ? [
'country-code' => key($settings['allowed_countries']),
] : [];
try {
$mobile_number = $util
->testMobileNumber($input['mobile'], $input['country-code']);
$verified = static::isVerified($element);
if ($op == 'mobile_number_send_verification' && !$util
->checkFlood($mobile_number, 'sms')) {
$form_state
->setError($element['mobile'], t('Too many verification code requests for %field, please try again shortly.', [
'%field' => $field_label,
]));
}
elseif ($op == 'mobile_number_send_verification' && !$verified && !($token = $util
->sendVerification($mobile_number, $settings['message'], $util
->generateVerificationCode(), $settings['token_data']))) {
$form_state
->setError($element['mobile'], t('An error occurred while sending sms.'));
}
elseif ($op == 'mobile_number_verify' && !$verified && $util
->checkFlood($mobile_number)) {
$verification_parents = $element['#array_parents'];
$verification_element = NestedArray::getValue($complete_form, $verification_parents);
$verification_element['verification_code']['#prefix'] = '<div class="verification show"><div class="description">' . t('A verification code has been sent to your mobile. Enter it here.') . '</div>';
NestedArray::setValue($complete_form, $verification_parents, $verification_element);
}
} catch (MobileNumberException $e) {
switch ($e
->getCode()) {
case MobileNumberException::ERROR_NO_NUMBER:
if (!empty($element['#required']) || $op) {
$form_state
->setError($element['mobile'], t('Phone number in %field is required.', [
'%field' => $field_label,
]));
}
break;
case MobileNumberException::ERROR_INVALID_NUMBER:
case MobileNumberException::ERROR_WRONG_TYPE:
$form_state
->setError($element['mobile'], t('The phone number %value provided for %field is not a valid mobile number for country %country.', [
'%value' => $input['mobile'],
'%field' => $field_label,
'%country' => !empty($countries[$input['country-code']]) ? $countries[$input['country-code']] : '',
]));
break;
case MobileNumberException::ERROR_WRONG_COUNTRY:
$form_state
->setError($element['mobile'], t('The country %value provided for %field does not match the mobile number prefix.', [
'%value' => !empty($countries[$input['country-code']]) ? $countries[$input['country-code']] : '',
'%field' => $field_label,
]));
break;
}
}
}
if (!empty($input['mobile'])) {
$input += count($settings['allowed_countries']) == 1 ? [
'country-code' => key($settings['allowed_countries']),
] : [];
if ($mobile_number = $util
->getMobileNumber($input['mobile'], $input['country-code'])) {
$country = $util
->getCountry($mobile_number);
if ($settings['allowed_countries'] && empty($settings['allowed_countries'][$country])) {
$form_state
->setError($element['country-code'], t('The country %value provided for %field is not an allowed country.', [
'%value' => $util
->getCountryName($country),
'%field' => $field_label,
]));
}
}
}
if (!empty($token)) {
$storage = $form_state
->getStorage();
$storage['mobile_number_fields'][$field_path]['token'] = $token;
$form_state
->setStorage($storage);
}
return $element;
}