2

Users need to fill link fields wrapped in a field group to their various social profiles like fb, twitter, pinterest. I need them to give any one of the fields as required since I use it to verify their profile.

I've tried conditional fields and field validation. Field validation require one of various fields gives me like seven "one of the fields is required" messages for all seven fields.

I tried different configurations with conditional fields without results.

Is there a workaround with any other modules?

1 Answer 1

3

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:

  1. Alter the form and add a custom validation function.
  2. All the submitted values will be available in $form_state in the validation function.
  3. Check the logic and trigger a form_set_error() to set the error.
1
  • I have it in my profile type attached to the registration form....Been trying all day and your answer has been be more than helpful. i don't know to code but i do understand the logic and will try to learn something soon. You've been an inspiration. Thanks Neo.
    – jack
    Commented Oct 5, 2013 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.