7

I need to manually render a number field in Drupal 7 on a custom form. I have created the following field definition:

$form['num_seats'] = array(
  '#type' => 'textfield',
      '#title' => t('Number of Seats'),
      '#required' => TRUE,
      '#number_type' => 'decimal',
      '#field_name' => 'num_seats',
      '#element_validate' => array ('number_field_widget_validate'),
      '#field_parents' => array(),
      '#language' => $language->language,
      '#field_prefix' => '$',
      '#size' => 14,
      '#maxlength' => 12,
      '#default_value' => $location ? $location['num_seats'] : $node->field_workshop_num_seats[$node->language][0]['value'],
);

However submitting the form produces an error of the form: "preg_replace() [function.preg-replace]: Compilation failed: range out of order in character class at offset 14 in number_field_widget_validate() (line 391 "

If I use 'integer' instead of 'decimal' for 'number_type', it works fine. I checked Number module's code and could see this is bcause of regex not being created correctly from field definition.

So my question is how can I manually render Fields outside of regular Drupal entity_type context (i.e. outside node/user etc. forms) in a custom form whose submission gets stored to a custom table.

2 Answers 2

3

After some googling and a bit of thought of how I've used stuff in the past. You're confusing a FAPI element with a Field Widget. They are not the same thing, this has been discussed before on Drupal.StackExchange. And is fully answered there in terms of why they are different.

I think your easiest solution is to create your own #element_validate function and not rely on the validate function provided directly by number_field_widget_validate -- but I would copy its guts essentially and create your own validate function from it. The preg_replace is failing because your FAPI element is not a Widget and has no $field['settings']. That's what I think is going on.

1
  • Yeah, it was appearing to me Field Widgets can't be used directly on regular Drupal forms as FAPI elements. BTW, I had a look at Number module's code and realized it expected 'settings' element in a field. I tried all possible combinations of specifying 'settings' and 'decimal_separator' inside it to somehow have number module recognize it and generate correct regex for validation but unfortunately did not work out. Anyways, thanks for replying..
    – r_honey
    Commented Oct 6, 2012 at 7:48
1

I guess after 3 years something changed. As I was able to add a decimal field without error as follow:

'location_crossing_mileage' => array(
    '#type' => 'textfield',
    '#title' => t('Mileage'),
    '#size' => 5,
    '#maxlength' => 12,
    '#number_type' => 'decimal',
    '#field_name' => 'location_crossing_mileage',
    '#element_validate' => array ('number_field_widget_validate'),
  ),
1
  • 4
    Yes, but notice the question was not about creating a field, but rather creating a form with Form API. There is no #number_type property in Form API. The title was misleading.
    – argiepiano
    Commented Oct 28, 2015 at 4:06

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.