Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

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… Read More
Author: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web