I tried implementing the email field using:

  $form['personalinfo']['email'] = array(
    '#type' => 'email',
    '#title' => t('Email'),
    '#required' => TRUE,
    '#default_value' => "",
    '#description' => "Please enter your email.",
    '#size' => 20,
    '#maxlength' => 20,
  );

However, nothing gets rendered. Could you provide some docs and examples on usage?

Comments

Nephele’s picture

I'm guessing that the main problem here is a bug in the alpha2 code -- see #949970: No default/plain formatter, Email contact form doesn't work. Patching the code should make it possible to get something to render (at least with the default and plain formatting options).

les lim’s picture

Status: Active » Closed (won't fix)

You can't use fields like that with Form API. The "#type" property refers to an element type, not a field type, and this module does not define any new element types (it makes use of the existing textfield element type).

kenorb’s picture

Category: support » feature
Status: Active » Closed (won't fix)

So the question is: What's the correct example to use Email field programatically?

Regarding element type:
http://api.drupal.org/api/drupal/modules%21system%21system.api.php/funct...
http://nodesforbreakfast.com/article/2012/02/24/create-custom-form-api-e...

kenorb’s picture

Category: feature » support
Status: Closed (won't fix) » Active
les lim’s picture

Category: feature » support
Status: Closed (won't fix) » Active

I suppose that depends: what is the use case for implementing Email Field programmatically? If it's just to implement a Form API textfield that validates email addresses, that can be done without the module:

$form['personalinfo']['email'] = array(
    '#type' => 'textfield',
    '#title' => t('Email'),
    '#required' => TRUE,
    '#default_value' => "",
    '#description' => "Please enter your email.",
    '#size' => 20,
    '#maxlength' => 20,
    '#element_validate' => array('MODULE_email_element_validate'),
);

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

I'm not sure it's in the scope of this module to do the above, but a maintainer might disagree.

kenorb’s picture

Or we could add the element_info():


/**
 * Implements hook_element_info().
 */
function email_field_element_info() {
  $types['email'] = array(
    '#input' => TRUE,
    '#size' => 60,
    '#maxlength' => 128,
    '#autocomplete_path' => FALSE,
    '#process' => array('ajax_process_form'),
    '#theme' => 'textfield',
    '#theme_wrappers' => array('form_element'),
  );
  return $types;
}
chop’s picture

Category: support » feature

I'm with you kenorb, why don't we add an email field type? How about you roll up a patch and let's see if we can't get this sucker into the core form API. It seems as elementary as having a password field type.

Albert.Liu’s picture

Issue summary: View changes

Maybe you can refer to drupal core system contact module snippets below:

<?php
// form
$form['mail'] = array(
  '#type' => 'textfield',
  '#title' => t('Your e-mail address'),
  '#maxlength' => 255,
  '#default_value' => $user->uid ? $user->mail : '',
  '#required' => TRUE,
);

// form validate
if (!valid_email_address($form_state['values']['mail'])) {
  form_set_error('mail', t('You must enter a valid e-mail address.'));
}
?>
jeroent’s picture

Status: Active » Needs review
StatusFileSize
new1.89 KB

As suggested by kenorb, I added an emailfield using hook_element_info + some extra changes.

Patch attached.

DrCord’s picture

This patch seems to work great! Thanks. This should certainly be included in this module if not in core forms.

DrCord’s picture

Status: Needs review » Reviewed & tested by the community
tomogden’s picture

Title: Example on usage of email field with the form api » Usage of email field with the form api

#9 patch passes my tests. It solves some very basic needs in a way that is simpler and more future-proof than anything else.

tomogden’s picture

Component: Documentation » Code
tomogden’s picture

StatusFileSize
new2.85 KB

It seems the patch at #9 has an odd line count error which prevents it from being applied automatically. I have rerolled it and fixed a couple of minor format items.

tomogden’s picture

Status: Reviewed & tested by the community » Needs review
tomogden’s picture

StatusFileSize
new2.85 KB

One more time.

bceyssens’s picture

Status: Needs review » Reviewed & tested by the community

Seems to work for me

nikita petrov’s picture

Why this functionality is still not in the release? Many people needs this functionality, why they should apply some patches in almost a year?

weri’s picture

Needed functionality for this type of field. Patch #16 works as expected.

Hope this gets added in the next release.

rcodina’s picture

Works for me too. This is clearly RTBTC, why it isn't committed yet?

chrisrockwell’s picture

StatusFileSize
new3.01 KB

Should this be updated to add a widget type "Email", similar to what d8 has done (https://www.drupal.org/files/drupal8.email-field.29.patch)? For backwards compatibility the textfield type would have to be allowed as well.

nickbits’s picture

Can confirm patch #16 works for me.

hugronaphor’s picture

Is the maintainer of this module alive? Can we move it forward?

fool2’s picture

+1 for this patch, still works!