14

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?

6 Answers 6

16
$form['my_element'] = array(
    '#type' => 'textfield',
    '#attributes' => array(
        ' type' => 'number', // insert space before attribute name :)
    ),
    '#title' => 'My number',
    '#required' => true,
    '#maxlength' => 3
);
5
  • 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, 2017 at 12:09
  • max length is not working Oct 5, 2017 at 9:08
  • 2
    Why does it ned to have a space?
    – fritzmg
    Mar 28, 2018 at 14:41
  • 1
    This leads to invalid HTML btw.: <input type="number" type="text">
    – fritzmg
    Mar 28, 2018 at 14:42
  • 1
    You can now use '#type' => 'number', in Drupal 8/9 May 24, 2022 at 16:37
7

As noted by @fritzmg , the Number element is included in core as of Drupal 8.0.

For example:

$form['phone_number']['areacode'] = array(
  '#type' => 'number',
  '#title' => '---',
  '#required' => true,
);
1
  • There is an interesting option that is #step : '#step' => '.1' allows a precision of 1 decimal : 0.1, 0.2, ... '#step' => '.01' would allow a precision of 2 decimals like 23,23 or 56,76 etc
    – romain ni
    Jun 1, 2023 at 7:18
5

You can install and use module Elements for it.

3
  • This is the correct way to get a HTML5 number field.
    – Decipher
    Sep 16, 2015 at 6:04
  • 1
    #type="numberfield" is the correct way to adjust the field when using this module.
    – YPCrumble
    Jul 12, 2017 at 17:14
  • 2
    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, 2018 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']) . ' />';
}
2
  • I was thinking the same thing for a solution -- but what do you mean it won't work with an invalid #value??? Nov 17, 2014 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. Nov 18, 2014 at 12:20

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.