13

I am trying to add a "number" field type to a form using FAPI:

$form['phone_number']['areacode'] = array(
  '#type' => 'textfield',
  '#title' => '---',
  '#width' => '30%',
  '#align' => 'center',
  '#required' => true,
  '#maxlength' => 3
);

I change the TYPE to "number" and the field doesn't get generated at all. The number module is enabled. I have implemented the following theme functions:

  • MYTHEME_form_element
  • MYTHEME_textfield
  • MYTHEME_container

What could be behind this field not showing up when I use #type = number or #type = number_integer, etc?

This might have something to do with it:

Creating Number fields (integer and decimal) manually in code for custom forms

However I actually want the type to render as "number" in HTML so smart phones show a numeric dialer

Any ideas?

13
$form['my_element'] = array(
    '#type' => 'textfield',
    '#attributes' => array(
        ' type' => 'number', // insert space before attribute name :)
    ),
    '#title' => 'My number',
    '#required' => true,
    '#maxlength' => 3
);
  • What's not clear? "Numeric field type in Form API" is need? Define attribute " type" (with space) in #attributes array with a desired value: number, date, color... dirty hack – Arthur Feb 11 '17 at 12:09
  • max length is not working – logeshvaran Oct 5 '17 at 9:08
  • 1
    Why does it ned to have a space? – fritzmg Mar 28 '18 at 14:41
  • This leads to invalid HTML btw.: <input type="number" type="text"> – fritzmg Mar 28 '18 at 14:42
5

You can install and use module Elements for it.

  • This is the correct way to get a HTML5 number field. – Decipher Sep 16 '15 at 6:04
  • 1
    #type="numberfield" is the correct way to adjust the field when using this module. – YPCrumble Jul 12 '17 at 17:14
  • 1
    This module is already included in the core in Drupal 8. The correct way to create a number field is by using '#type' => 'number' (not 'numberfield') in the render array. – fritzmg Mar 28 '18 at 14:47
2

I'm a bit late to the party. The problem is that theme_textfield (D7: includes/form.inc , lines 3924ff) hard-codes $element['#attributes']['type'] = 'text';.

You may wrap said call in a if (!isset($element['#attributes']['type'])) { ... } or wait for the patch https://www.drupal.org/node/2545318 to be applied.

1

this is your code below..

$form['phone_number']['areacode'] = array(
'#type' => 'textfield',
'#title' => '---',
'#width' => '30%',
'#align' => 'center',
'#required' => true,
'#maxlength' => 3
);

please tell me what is ['phone_number'] , it's a container or not or its exist in your form or not and if you want to create custom field in your form then you should use hook_form_alter hook_form_alter

like

  function mymodule_form_alter($form_id, &$form) {
 if ($form_id == 'form_id') {

$form['field_name'] = array(
 '#type' => 'textfield',     
 '#title' => t('title'),
 '#description' => t('Description.'),
 '#width' => '30%',
 '#align' => 'center',
 '#required' => true,
 '#maxlength' => 3
 );
  }
   }

hope this help you..

0

I don't see it working with invalid value for the #type field, however you can do something like that:

The form element:

$form['my_element'] = array(
    '#type' => 'textfield',
    '#attributes' => array(
        'data-type' => 'number',
    ),
    '#title' => '---',
    '#width' => '30%',
    '#align' => 'center',
    '#required' => true,
    '#maxlength' => 3
);

And implementing theme_textfield

if(isset($element['#attributes']['data-type']) && $element['#attributes']['data-type'] === 'number'){
    $output = '<input type="number" />';
}else{
    $output = '<input' . drupal_attributes($element['#attributes']) . ' />';
}
  • I was thinking the same thing for a solution -- but what do you mean it won't work with an invalid #value??? – Alex.Barylski Nov 17 '14 at 16:02
  • Well if you don't put a valid value for the #type the form element won't be rendered at all, as you stated before. – Victor Lazov Nov 18 '14 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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