"Content profile registration broken": debugging rogue form validate overrides
Bad coding in a development partner's custom module was the culprit. It was overriding content_profile_registration's setting of a validation function (and every other module's, too).
Inside the custom module's implementation of hook_form_alter() lives the error:
<?php
switch ($form_id) {
// ... ignore lots of code
case 'user_register':
// ... ignore more code
// here's the big ugly error
$form['#validate'] = array('pduser_username_validate');
break;
// ... ignore some more code
}
?>
Note also that the switch on form_id is somewhat deprecated and can be replaced with hook_form_user_register_alter() in this case. See http://api.drupal.org/api/function/hook_form_FORM_ID_alter/6
before:
<?php
'#validate' =>
array (
0 => 'user_register_validate',
1 => 'content_profile_registration_user_register_validate',
),
?>
after-- the altered form #validate:
<?php
array (
0 => 'customuser_username_validate',
)
?>
This was tracked down by drupal_set_message code mostly outputting for form_alter, but zeroed in on when similar var_export code in content_profile_registration's validate function for the user registration form utterly failed to output anything. After visiting 221B Baker Street, London, we deduced that the function wasn't being called at all:
<?php
function content_profile_registration_user_register_validate($form, &$form_state
) {
// ...code removed
drupal_set_message('aqui'); //@debug
drupal_set_message('<pre>'.var_export($form_state,TRUE).'</pre>');
print 'anger';
}
?>
The right way
Because we know that user module at least has already added a validation function, we can use the array addition notation to add an additional one.
<?php
$form['#validate'][] = 'customuser_username_validate';
?>
See Unset certain values in an unkeyed PHP array to unset one validator but keep any others.
Comments
same problem
Hello,
I installed the 'profile' module out of drupal 6.x core to add things like first name, last name, etc to the user registration form. However, there is no real validation on these fields. In other words, for a text field (first name), I can enter something like '123456'. So my question is, where is it appropriate to use 'hook_form_alter' to modify the validation? Is something that should be handled in template_preprocess ? Where is it appropriate to override the profile module?
Thanks.
Post new comment