1

In my form i have this element :

   $form['poidsMin'] = array(
        '#type' => 'number',
        '#title' => $this->t('Poids minimum en gramme'),
        '#default_value' => $frais_port_entity->get('poidsMin'),
        '#description' => $this->t("Poids minimum du produit"),
        '#required' => TRUE,
    );

When i put 10 or 20, all is good but if i put 10.3 or 10,3 drupal 8 jquery validate tell me to insert a value like 10 or 11 with no comma.

My field in mysql is a float, how can i disable this validation for number type field ?

1 Answer 1

3

Browser support may vary, but essentially you want to add an attribute of step="any":

$form['poidsMin'] = array(
    '#type' => 'number',
    '#title' => $this->t('Poids minimum en gramme'),
    '#default_value' => $frais_port_entity->get('poidsMin'),
    '#description' => $this->t("Poids minimum du produit"),
    '#required' => TRUE,
    '#attributes' => ['step' => 'any'],
);
6
  • Browser support for decimal separator in number field is horrible. It really works only for integer. For float use text field and validate yourself.
    – 4uk4
    Mar 31, 2016 at 10:09
  • Or use pattern="\d+(\.\d*)?" or something similar @4k4
    – Clive
    Mar 31, 2016 at 10:11
  • In UK that may work, but most of Europe uses a comma. All versions of IE and Edge can't handle this. Chrome and Firefox are almost there. IOS works perfect.
    – 4uk4
    Mar 31, 2016 at 12:05
  • That's fine @4k4, the regex was just an example, it can be extended to support regional differences. Not sure where those browser stats have come from, IE10 & 11 support it, as does every other browser barring Opera Mini: caniuse.com/#feat=input-pattern. Safari (including iOS Safari) is actually one of the ones with only partial support
    – Clive
    Mar 31, 2016 at 12:16
  • @Clive, I meant support for number fields. Pattern does not help much here. You want to use pattern with a text field?
    – 4uk4
    Mar 31, 2016 at 12:44

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.