materio expo added email validation, file without email redirect to form
This commit is contained in:
@@ -7,12 +7,38 @@ use Drupal\Core\Form\FormStateInterface;
|
||||
// use Symfony\Component\HttpFoundation\Cookie;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Drupal\Core\Url;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Egulias\EmailValidator\EmailValidator;
|
||||
|
||||
/**
|
||||
* Class GetEmail.
|
||||
*/
|
||||
class GetEmail extends FormBase {
|
||||
|
||||
/**
|
||||
* The email validator.
|
||||
*
|
||||
* @var \Egulias\EmailValidator\EmailValidator
|
||||
*/
|
||||
protected $emailValidator;
|
||||
|
||||
/**
|
||||
* Constructs
|
||||
*
|
||||
* @param \Egulias\EmailValidator\EmailValidator $email_validator
|
||||
* The email validator.
|
||||
*/
|
||||
public function __construct(EmailValidator $email_validator) {
|
||||
$this->emailValidator = $email_validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container) {
|
||||
return new static($container->get('email.validator'));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@@ -80,6 +106,14 @@ class GetEmail extends FormBase {
|
||||
*/
|
||||
public function validateForm(array &$form, FormStateInterface $form_state) {
|
||||
parent::validateForm($form, $form_state);
|
||||
$values = $form_state->getValues();
|
||||
if (!$this->emailValidator->isValid($values['email'])) {
|
||||
$form_state->setErrorByName('email',
|
||||
$this->t('%email is an invalid email address.', [
|
||||
'%email' => $values['email'],
|
||||
])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user