Drupal registration - add validation to every field
I would like to add a validation function to every form field on the Drupal registation page, including the custom Profile fields I've added.
Is there a 开发者_如何学编程simple way to do this without manually attaching it to each field? Would like it to support future profile fields that may be added without having to manually update the code.
Depending on the complexity of the validation, the Validation API module may be able to do what you need. Be aware that the module is no longer being actively maintained though.
Depending on your needs, the Clientside Validation module might also work, though the rules wouldn't be applied to users with Javascript disabled.
I'm assuming you're on D6.
You can kinda see what you need to validate here:
http://api.drupal.org/api/drupal/modules--profile--profile.module/function/profile_form_profile/6
If you implemented something with hook_elements you could validate anything the profile module creates. It shouldn't be too hard if you've got some good developer skills.
http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_elements/6
You can access the form with a hook_form_alter, and add one of more additional form validators.
Make sure you don't overwrite your default validator, but just add an extra one.
$form['#validate'][] = 'my_custom_validate';
This extra validate handler could loop over the profile fields and check for empty entries for example, or something else.
精彩评论