Where are these fields attached to? User registration page or content type?
Anywhere it might be, there must be a form, and what you need to do is add a form level validation function, not a field level validation. Field level validation works well, when logic is associated with that particular field but for your case you need to add form level validation handler:
Here is an example:
/**
* Implementation of hook_form_alter().
*/
function yourmodulename_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'user_register': // or whatever is the form id in which you want to apply validation
// this line adds a new validation function
$form['#validate'][] = 'custom_data_validate'; //or whatever validation function name
break;
}
}
/**
* Custom validation function.
*/
function custom_data_validate(&$form, &$form_state) {
$values = $form_state['values']; //all submitted values will be in this array, keyed by the name, grab any value you want.
if (empty($values['facebook']) && empty($values['twitter'])) {
form_set_error('facebook', t("you need to fill up either of the two fields"));
}
}
I did not take care of the logic and the code provided is just an example but the basic workflow is:
- Alter the form and add a custom validation function.
- All the submitted values will be available in $form_state in the validation function.
- Check the logic and trigger a form_set_error() to set the error.