Hi,

I have come across that if I create a numeric webform field programmatically, it is not rendering on the page. The code I used for this purpose is:

$form['access_required']['desk'] = array(
    '#type' => 'number',
    '#title' => t('Desk Number'),
	'#required' => TRUE,
	'#attributes' => array(
      'min' => '0',
      'max' => '',
    ),
    
);	

As you can see above code is very simple and if I change type as 'textfield', it works fine. Please let me know if I doing something wrong.

Thanks

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

crystalgrafix created an issue. See original summary.

tamnv’s picture

Hi @crystalgrafix,

The code should be like this:

$form['access_required']['desk'] = array(
    '#type' => 'webform_number',
    '#title' => t('Desk Number'),
    '#required' => TRUE,
    '#attributes' => array(
      'min' => '0',
      'max' => '',
    ),
);

It means you should use '#type' => 'webform_number'

crystalgrafix’s picture

Thanks @tamnv it works!

I didn't know 'webform_' needs to be there as a prefix which is bit different to the way other types are used.

crystalgrafix’s picture

Status: Active » Fixed
crystalgrafix’s picture

Status: Fixed » Needs work

Even if the numeric text field works, I still cannot get the label to work. Field appears without a label.

Can you please guide me to some sort of a documentation?

tamnv’s picture

Status: Needs work » Active
FileSize
790 bytes

Hi @crystalgrafix,

I created a patch for webform module. I think it's a missing from webform module. You can try it.

tamnv’s picture

Status: Active » Needs review
DanChadwick’s picture

Status: Needs review » Closed (works as designed)

We don't use html5 number support because browsers do not return consistent results with various locale-specific thousand and decimal separators. Until browsers can handle these correctly and consistently, we're not using number. But feel free to use the patch if you don't mind the browser compatibility issues.

crystalgrafix’s picture

Status: Closed (works as designed) » Needs work

@DanChadwick if that is the case, what would you recommend to create a field with min max values? If I create the field from the webform admin panel, I can give these values but it's a shame that the same is not supported from API code.

crystalgrafix’s picture

@DanChadwick validation for min, max values are not applied the same way if I use textfields inside webform API. However, if you use webform admin panel, it works well. Isn't this a bug in API?

Dev1.addweb’s picture

Status: Needs work » Needs review
FileSize
518 bytes

Apply the patch (webform_api_number-2799661-11.patch) to add validation for min, max values of textfields inside webform API and then you can use following code to make things work.

$form['access_required']['desk'] = array(
  '#type' => 'webform_number',
  '#title' => t('Desk Number'),
  '#required' => TRUE,
  '#min' => '0',
  '#max' => '',
);

Let me know incase of any concerns.

DanChadwick’s picture

@crystalgraphix: The min and max are validated in the server. I'm not sure what you mean by "the API code". Unfortunately html5 support for numbers isn't there yet.

crystalgrafix’s picture

@DanChadwick: Min and max values are validating when only used through the webform admin panel. If you try the below code programmatically, it won't work. So my alternative was to go with a custom validate function. My custom code was referred to as 'API code' in my previous post.

$form['access_required']['desk'] = array(
'#type' => 'webform_number',
'#title' => t('Desk Number'),
'#required' => TRUE,
'#attributes' => array(
'min' => '0',
'max' => '',
),
);

Liam Morland’s picture

I don't understand the problem. Can you describe how to reproduce it? Do either of the patches in this issue solve it for you?

Liam Morland’s picture

Status: Needs review » Closed (works as designed)

If there is a bug here, please provide steps to reproduce the problem and include the desired behavior and the actual behavior.