0

I have a textfield in a form that must contain a number. Using an integer for the field type is not possible - it must be text; however, I need to ensure that the input is actually a number. I've used:

function myfunc_form_user_register_form_validate($form, &$form_state) {
  if (!is_numeric($form_state['values']['number'])) {
    form_set_error('number', t('Field must be Numeric'));
  }
}

However, it does not work, that is, no errors are ever displayed. I have implemented the above code in template.php. I know there are Form Validation modules; however, I would like to do such a simple thing myself. I also think it's a bit overkill to create a whole module to simply validate a single field in a single form! I also wonder how to find the $form[] keys. I named the input field number so I imagine that's the key but it's just a guess. I've looked through the Form API and it is not very clear what I am supposed to do. Simple things, such as finding out what's even in $form[] alludes me and it doesn't seem to be clearly explained.

5
  • How have you attached the validate handler to the form? Are you 100% sure myfunc_form_user_register_form_validate has been invoked?
    – Clive
    Commented Nov 5, 2015 at 13:04
  • 1) I assume so: $form['#validate'][] = "myfunc_form_user_register_form_validate"; 2) Yes, it is being called.
    – monster
    Commented Nov 5, 2015 at 14:19
  • I would check $form_state['values']['number'] in that case - as far as PHP is concerned, the string doesn't contain a valid number (that's the only way is_numeric would fail)
    – Clive
    Commented Nov 5, 2015 at 14:23
  • Sorry Clive, I made an error: the method is not being called! The form_alter method is running ($form['#validate'][] = "myfunc_form_user_register_form_validate";
    – monster
    Commented Nov 5, 2015 at 14:26
  • No worries, that's actually a more straightforward method to accomplish this anyway, I've chucked a quick answer in
    – Clive
    Commented Nov 5, 2015 at 14:27

2 Answers 2

4

Drupal already has a helper for this, in the shape of the element_validate_number() validation callback:

function MYMODULE_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  $form['number']['#element_validate'][] = 'element_validate_number';
}

You can also use element_validate_integer() or element_validate_integer_positive if either of those make more sense for your use case.

3
  • Unfortunately, the method is calling; however, it is not validating. I think the problem could be number. I named the field number so I assume that's the key of the array? The fieldname is field_number. How do I know what key to use? Similarly, how do I find out other elements keys?
    – monster
    Commented Nov 5, 2015 at 14:31
  • I now get this error: Notice: Undefined index: #value in element_validate_number() (line 4385 of C:\xampp\htdocs\drupal\includes\form.inc).
    – monster
    Commented Nov 5, 2015 at 15:15
  • Oh right, yeah that would've been helpful to know ;) So it'll be more like $form['field_number'][LANGUAGE_NONE][0]['value']['#element_validate'][] = ...
    – Clive
    Commented Nov 5, 2015 at 15:37
0

You need to define your validate function in hook_form_<form_id>_alter($form, $form_state)

like the following

<themeName>_form_user_register_form_alter($form, $form_state) {
// your rest of the code
$form['#validate'][] = "myfunc_form_user_register_form_validate";
}

Clear the cache afterwards and check if the function is properly called

Hope this will help!

1
  • This seems to work. However, if (!is_numeric($form_state['values']['number'])) { in my method doesn't validate correctly. Instead I have to make it: is_numeric($form_state['values']['number']['und'][0]['value'])
    – monster
    Commented Nov 5, 2015 at 15:27

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.