Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
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
Comment | File | Size | Author |
---|---|---|---|
#11 | webform_api_number-2799661-11.patch | 518 bytes | Dev1.addweb |
| |||
#6 | webform_number_label-2799661-6.patch | 790 bytes | tamnv |
|
Comments
Comment #2
tamnv CreditAttribution: tamnv commentedHi @crystalgrafix,
The code should be like this:
It means you should use '#type' => 'webform_number'
Comment #3
crystalgrafix CreditAttribution: crystalgrafix commentedThanks @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.
Comment #4
crystalgrafix CreditAttribution: crystalgrafix commentedComment #5
crystalgrafix CreditAttribution: crystalgrafix commentedEven 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?
Comment #6
tamnv CreditAttribution: tamnv commentedHi @crystalgrafix,
I created a patch for webform module. I think it's a missing from webform module. You can try it.
Comment #7
tamnv CreditAttribution: tamnv commentedComment #8
DanChadwick CreditAttribution: DanChadwick commentedWe 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.
Comment #9
crystalgrafix CreditAttribution: crystalgrafix commented@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.
Comment #10
crystalgrafix CreditAttribution: crystalgrafix commented@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?
Comment #11
Dev1.addweb CreditAttribution: Dev1.addweb at AddWeb Solution Pvt. Ltd. commentedApply 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.
Let me know incase of any concerns.
Comment #12
DanChadwick CreditAttribution: DanChadwick commented@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.
Comment #13
crystalgrafix CreditAttribution: crystalgrafix commented@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' => '',
),
);
Comment #14
Liam MorlandI don't understand the problem. Can you describe how to reproduce it? Do either of the patches in this issue solve it for you?
Comment #15
Liam MorlandIf there is a bug here, please provide steps to reproduce the problem and include the desired behavior and the actual behavior.