Hi, I have been trying to override one of the field validation of the forward form through a hook_form_alter in order to change the error message without changing the function in the forward.module file, unfortunately I have had no luck achieving this. This is what I have done so far.
In the forward.module file there is a conditional (shown below) inside the "forward_form_validate" function (around the line 733) that validates the field "name" of the form and displays the message "You must enter your name." if the user does not fill out the field.
if (!$form_state['values']['name'] ) {
form_set_error('name', t('You must enter your name.'));
}
Let's say that I want to modify the validation of this field and display a different error message instead of the one shown above, something like "Please type your name" and I want this to happen only when using one particular theme. So I've added the code below into the file "template.php" that resides inside the theme's foder:
function mytheme_form_alter(&$form, &$form_state, $form_id) {
if($form_id == 'forward_form') {
$form['#validate'][] = 'my_custom_validation';
}
}
function my_custom_validation($form, &$form_state) {
if (!$form_state['values']['name'] ) {
form_set_error('name', t('Please type your name.'));
}
}
The hook_form_alter works, but it does not overrides the validation made in the forward.module file. I know it works because if I comment out the conditional with the form_set_error('name', t('You must enter your name.')); in the forward.module, I get my custom field validation along with the message from the template.php, however, if the same validation appears in the module.forward, it ignores my custom validation.
I wonder if anyone can help me out to solve this, I would really appreciate it. I am still learning Drupal and many of the terms that I've used to explain this issue might not be correct so please feel free to ask in case of having any doubt about this request.
Comments
Comment #1
begun commentedI am not familiar with this particular module but from your description above it sounds like what you have done is added an additional validation function to any existing ones for that particular form. This would result in your function "my_custom_validation" being run after the default validation function.
What you might be able to do is replace the default function with your own one. You could try something like this: Unset the default validation function for the Forward module in your mytheme_form_alter() function, as well as adding your custom function to the form (also done in your mytheme_form_alter() function). Then copy and paste the content of the default function (from the Forward module) into your custom function. Then modify the code in your custom function the way you want it.
It may be that the default validation function refers to other functions in the Forward module. If this turns out to be the case you might need to include these using form_load_include() in your mytheme_form_alter() function.
The solution I provide is a theoretical one. I don't know how practical it will be in your case, as I am not familiar with the Forward module and its validation function. But hopefully it will give you an option to explore.
Comment #2
john.oltman commented