Validate at least one field to be filled in Gravity Form

How can I make one of two fields required (ie you have to indicate either the telephone or the email address)?

Assuming your form ID is 5 and the 2 fields to check are date field ID is 8 and 18, add this code to your plugin or function file:

<?php
/*
* Make one of two fields required
*/
add_filter('gform_validation_5','wpster_custom_validation');
function wpster_custom_validation( $validation_result ) {
  $form = $validation_result['form'];
  if(empty(rgpost('input_8')) && empty(rgpost('input_18'))) {
    foreach($form['fields'] as &$field) {
      if ($field->id=='8' || $field->id=='18') {
        $field->failed_validation = true;
        $field->validation_message = 'Veuillez indiquer votre adresse e-mail ou votre numéro de téléphone';
        $validation_result['is_valid'] = false;
      }
    }
  }
  $validation_result['form'] = $form;
  return $validation_result;
}
?>

Submit a Comment

Your email address will not be published. Required fields are marked *