Hoppa till huvudinnehåll
Menu

Den här sidan använder cookies. Om du fortsätter använda sidan så accepterar du användandet av cookies. Läs mer om cookies

Jag förstår

Email validation using dependency injection in Drupal 8

The dependency injection is the best way for using services in Drupal 8 and it should be used whenever possible.

In Drupal 7 we were using the function valid_email_address for validating emails in Forms, like:

function test_custom_form_validation($form, &$form_state) { 
   $mail = $form_state['values']['email']; 
   if (!valid_email_address($mail)) { 
      form_set_error('email', t('Please Enter a valid email address.')); 
   } 
}

We should not used this method for validating emails in Drupal 8 because it is deprecated. Instead, it is recommended to use dependency injection. The dependency injection is the best way for using services in Drupal 8 and it should be used whenever possible. The best practice is to call the require service as argument in the constructor. It is not recommended to call out the global service container what it is a common error when you are starting to develop with Drupal 8.

Now, I will explain how to implement it.

Include in your Form (my_module/src/Form/MyForm.php) a new variable:

/**
 * The email validator.
 *
 * @var \Egulias\EmailValidator\EmailValidator
 */
 protected $emailValidator;

So, you will need to include:

use Egulias\EmailValidator\EmailValidator;

In the next lines, I show you how to use this variable for doing the validation:

/**
* Construct.
* @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}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
//building my form...
   $form['emailaddress'] = array(
      '#type' => 'email',
      '#title' => $this->t('Email'),
      '#attributes' => array('class' => array('email-input'), 'disabled' => 'disabled'),
      '#default_value' => isset($result['emailaddress']) ? $result['emailaddress'] : '',
      '#maxlength' => 80,
    ); 
    return $form; 
} 

/**
 * @inheritDoc
   */
public function validateForm(array &$form, FormStateInterface $form_state) { 
    parent::validateForm($form, $form_state); 
    // Validate email. 
    $emailaddress = trim($form_state->getValue('emailaddress')); 
    if (!$this->emailValidator->isValid($emailaddress)) { 
        $form_state->setErrorByName('emailaddress', $this->t('%emailaddress is an invalid email address.', array('%emailaddress' => $emailaddress))); 
     } 
}

More info in Drupal.org

Vi hjälper dig nå resultat. Kontakta oss Ring direkt på 08-20 90 04.

Cache in Drupal 8: How it worked, and what we wanted to be solved in D8

If you just want your content to be cached before Drupal 8, there were almost no problems, just turn on caching for anonymou…
MDN

Cache in Drupal 8: Introduction

In a series of blog posts we will go through how caching works in Drupal 8, to try to demystify the caching layer in Drupal …
MDN

Updating taxonomy term name in Drupal 8

Here you can read how to use hook update for updating taxonomy terms in Drupal 8.