5

I'm doing some mobile forms with Drupal and have an email address textfield that currently has type="text" in the resulting HTML. I'd like this to be type="email" instead so that iOS shows a more useful keyboard when entering the email address.

I tried adding:

"#attributes" => array("type" => "email")

to my field but it seems to just get ignored by Drupal. Is there a correct and/or supported way to achieve this?

5 Answers 5

5

As it stands, Drupal core doesn't support all HTML5 form elements. The Elements module will add some support though, so check that out.

HTML5 elements (url, email, search, tel, number, range)

5
  • I'm not sure about that to be honest. It'd work, but it seems kinda hacky.
    – Chapabu
    Oct 8, 2012 at 17:35
  • Doesn't work for Drupal 7.x
    – mate64
    Jun 13, 2013 at 13:50
  • @cept0 You'll have to be a bit clearer - what doesn't work?
    – Chapabu
    Jun 13, 2013 at 14:01
  • The elements module does support #attributes though. Unfortunately, I can't vote your answer down atm. But I will do so asap
    – mate64
    Jun 13, 2013 at 14:37
  • 1
    I think you're misreading things. The OP asked for the ability to put in an email field. The Elements module does that. Why would you downvote it?
    – Chapabu
    Jun 13, 2013 at 15:05
6

Elements which are input[type=email] are a builtin as of Drupal 8. They can be added (along with other HTML5 elements and markup) to Drupal 6 or 7 via the Elements module.

As of writing this answer, I found the Element-module docs quite scant; they don't explain that the method for getting an e-mail input-element is slightly different than how it's done in D8. For Drupal 7:

/** 
 * Implements hook_form().
 * "For long-lasting relief you can count on!"
 *
 * hook_form() functions are passed a form-node,
 * plus extra state-info if present.
 * 
 * At the end of the function, the form-node should
 * be returned to the caller.
 *
 */
function mymodule_form($node,&$form_state) {

    $node['primary_email'] = array(
        '#type' => 'emailfield',
        '#title' => t('Submitter'),
    ); // in Drupal 8, it's '#type' => 'email'

    return $node;
}

Be sure to have the Elements module enabled before you expect the above to work. Also, if you haven't already, you'll also want to make the form accessible by doing something like:

/**
 * Implements hook_menu().
 * "Money back guarantee!"
 */
function mymodule_menu() {

    // create primary menu-entry for module:
    $items['mymodule'] = array(
        'title' => 'My Very Own Module',
        'page callback' => 'mymodule_description',
        'access callback' => TRUE,
        'expanded' => TRUE,
    );

    // create menu-entry for form:
    $items['mymodule/myform'] = array(
        'title' => 'My Very Own Form',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('mymodule_form'),
        'access arguments' => array('Behold, the form.'),
        'file' => 'mymodule.module',
    );

    // ...[maybe add more stuff here]...

    return $items;

The other elements made available by Element are listed at cgit.drupalcode.org/elements/tree/elements.module#n7.

1

Instead of adding a form validation handler to the form, you can add a validation handler to the form element that needs to be validated:

$form['email'] = array(
  '#type' => 'textfield',
  '#title' => 'Email',
  '#required' => TRUE,
  '#element_validate' => array('myelement_email_validate')
);

The validation handler receives three arguments: The form element being validated, the $form_state and the $form array for the form containing the form element.

function myelement_email_validate($element, &$form_state, $form) {
  $value = $element['#value'];
  if (!valid_email_address($value)) {
    form_error($element, t('Please enter a valid email address.'));
  }
}
0

this is how i have done it:

 $form ['group_coordonnees'] ['field_candid_email'] = array (
        '#type' => 'textfield',
        '#title' => t ( 'E-mail' ),
        '#default_value' => '',
        '#required' => TRUE

);


$node->field_candid_email ['und'] [0] ['email'] = $form_state ['values'] ['field_candid_email'];

The field type of field_candid_email being selected in the back office as email

-1

on Drupal 7 you can:

 $form['msg'] = array(
   '#type' => 'text',
   '#attributes' =>array( 
     'placeholder' =>  'some text' , 
     'required' => "" ,  //required field
     'type' => "email" ,
   ),);  
1
  • 1
    There is no "type=text" in Drupal core form API. This solution won't work without an extra module. May 31, 2016 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.