materio expo added email validation, file without email redirect to form

This commit is contained in:
Bachir Soussi Chiadmi 2021-09-08 16:42:58 +02:00
parent ba010c366e
commit 2ffda1e0b2
6 changed files with 53 additions and 7 deletions

View File

@ -24,6 +24,7 @@ class QRController extends ControllerBase {
$session = \Drupal::request()->getSession();
$alreadysetemail = $session->get('materio_expo_email');
if($alreadysetemail){
//* get node from ref
$entity_storage = \Drupal::entityTypeManager()->getStorage('node');
$query = $entity_storage->getQuery()
@ -39,12 +40,19 @@ class QRController extends ControllerBase {
//* redirect to file
return new RedirectResponse($file->createFileUrl());
// return [
// '#type' => 'markup',
// '#markup' => $this->t("Error, email not provided"),
// ];
} else {
return [
'#type' => 'markup',
'#markup' => $this->t("Error, email not provided"),
];
// return [
// '#type' => 'markup',
// '#markup' => $this->t("Error, email not provided"),
// ];
$url = Url::fromRoute('materio_expo.get_email', ['ref'=>$ref]);
return new RedirectResponse($url->toString());
}
}

View File

@ -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'],
])
);
}
}
/**

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -3002,3 +3002,7 @@ img.lazy{
// background-color: red;
}
}
#better-messages-default{
max-width: 90%;
}