Hello I am trying to validate the field "profile_rut" from the "Personal" category. With a module I build called "sitehelper"

function sitehelper_form_alter($form_id, &$form) {
if ($form_id == 'user_register' || $form_id == 'user_edit') {
$form['Personal']['profile_rut']['#validate'] = array('formvalidate_rut' => array());
}
}

function formvalidate_rut($element) {
if ($element['#value'] == "123") {
form_set_error('profile_rut', t('RUT is invalid'));
}
}

Have tried this code and many modifications for long time, but cant make it work.
What I am doing wrong?

Comments

Chill35’s picture

Is this a Drupal 6 question?

Caroline

11 heavens.com

mortenson’s picture

Yes it is :)

Chill35’s picture

In Drupal 6, HOOK_form_alter takes 3 arguments [1], and to add validation to an element once must use '#element_validate' [2].

function sitehelper_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'user_register' || $form_id == 'user_edit') {
    $form['Personal']['profile_rut']['#element_validate'] = array('_sitehelper_profile_rut_validate');
  }
}

function _sitehelper_profile_rut_validate($form, &$form_state) {
  if ($form_state['values']['profile_rut'] == "123") {
    form_set_error('profile_rut', t('RUT is invalid'));
  }
}

The way element validation is handled varries through Drupal core. Looking for examples, I did not find consistency.
The above validation should work.

[1] http://api.drupal.org/api/function/hook_form_alter/6
[2] http://api.drupal.org/api/file/developer/topics/forms_api_reference.html...

Caroline

11 heavens.com

Chill35’s picture

To validate checkboxes, you'll need to install this module: http://drupal.org/project/checkbox_validate

Caroline

11 heavens.com

mortenson’s picture

Wujuu!!!
Caroline, I apreciated so much!

I have just test it and works.
Right now is late here, so tomorrow at first hour I will study with detail your post.

thanks again!

Chill35’s picture

It was late for me too... A few typos... this one: "and to add validation to an element, once one must use '#element_validate'..."

I'm glad it works :-)

Caroline

11 heavens.com

Chill35’s picture

That is...

If you want a required checkbox to be validated you'll need that module...

You can do your own validation of course.

Caroline

11 heavens.com

mooffie’s picture

Excellent answer.

resting’s picture

I'd created a few checkboxes and a textfield using the Profiles module.
Problem is I need to validate them (checks for filled entries) as a group.

Meaning either they check one of the boxes, or has anything entered in the textfield, it will go through.

Had been searching for hours and is still clueless where to do the modifications.
Drupal 5. Any clues?