views:

20

answers:

2

Is there anyway say Drupal to validate form elements like email fields, passwords, numeric fields validate automatically lets say bind a system validator

$form['email] = array(
   '#title' => t('Email'),
   '#type' => 'textfield',
   '#validate_as' => array('email', ...),
   ...
);

+1  A: 

Yep!

Though I have not experimented with it much.

http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6#element_validate


$form = array(
'#type' => 'fieldset',
'#title' => t('Input format'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => $weight,
'#element_validate' => array('filter_form_validate'),
);
...

Rimian
+3  A: 

Rimian is both correct and wrong.

The good thing as Rimian points out, is that you can attach any validation function to your form fields using the #element_validate.

However I'm not aware of a set of form api validation functions you can call to test most common things like, if the value is:

  • a int
  • a positive int
  • a valid date (such a function exists in the date module though)
  • a email-address (you can use valid_email_address to check the email, but you need a function to raise validation error)

So while you can do this, it's a bit more work than you were hoping for, as you will need to create these validation functions yourself. But once you have done this, you can reuse them with #element_validate.

The use of #element_validate is mostly centered around complex validation fx date validation, location validation and such, as it requires some work to create these validation functions. Most of the time, you don't need to validate that many numbers etc (which you quite easily could do within a normal validation function using a loop). So I'm not sure how much this will be of help to you, but it's definitely a possibility.

googletorp