1

I've tried to search for this issue here at StackOverflow, but the answers are quite generally speaking, so I'm having some trouble with knowing how to do this exactly.

I have a Contact Form 7 form with two fields: one for email called your-email and one for telephone called your-telephone. I want the form to accept either one of these fields, and to invalidate when both fields are empty.

I found a snippet that doesn't do exactly this, but seems to use the right hook for it, here: https://wpquestions.com/Cross_Field_Validation_in_Contact_Form_7/10078

I transformed the code to my purpose like this and it's working halfway:

add_filter( 'wpcf7_validate', 'wpq_validate' );
function wpq_validate( $result ) {

$email = filter_input( INPUT_POST, 'your-email', FILTER_SANITIZE_STRING );
$telephone = filter_input( INPUT_POST, 'your-telephone', FILTER_SANITIZE_STRING );

$error_msg = 'One of these fields must be entered';

if(  empty($email) && empty($telephone) ) {
$result['valid'] = false;
$result['reason']['your-email'] = $error_msg;
$result['reason']['your-telephone'] = $error_msg;
} 
return $result;

}

By "halfway", I mean that the form is not submitted when I leave both fields empty. But it's not showing any error messages and the loading spinner of the form just spins infinitely. I'm guessing there are some calls in the code that are deprecated, since the code was posted in 2014.

So does anyone know how I should change this according to the current version of Contact Form 7?

1
  • It's true that CF 7 updated its code base in 2015, so all solutions prior to that won't work anymore.
    – Hans
    Sep 19, 2018 at 14:36

1 Answer 1

1

I managed to work out the code with some inspiration from more updated snippets regarding the wpcf7_validate filter. This code prevents the form from submitting and shows two error messages below each of the two fields. It's not really DRY, and I would prefer if the form showed a general error below the whole form instead of showing errors on each field. So would love some tips from anyone who knows how to do that.

function wpq_validate( $result ) {

$form  = WPCF7_Submission::get_instance();
$email = $form->get_posted_data('your-email');
$telephone = $form->get_posted_data('your-telephone');

if(  empty($email) && empty($telephone) ) {
$result->invalidate('your-email', 'Either one of these fields must be filled. Please try again.' );
$result->invalidate('your-telephone', 'Either one of these fields must be filled. Please try again.' );
} 
return $result;

}
5
  • My form shows a message like "One or more fields have an error. Please check and try again." when I submit, additionally to showing the error for the specific field. Is that message gone since you implemented above solution?
    – Hans
    Sep 19, 2018 at 14:48
  • Nope, it's still there. But is it possible to invalidate the fields without showing an error for that specific field? I guess I can try to just not give the method any arguments. And if that works, I would still like to be able to customize a specific error message for this type of validation only, saying something like: "You must enter either your email or telephone number". And then show the default error message if the form is being invalidated for another reason, for example entering a wrongly formatted e-mail.
    – tobiasg
    Sep 19, 2018 at 14:52
  • If the general error message below the form is filterable, you might be able to add a filter for it in your conditional block.
    – Hans
    Sep 19, 2018 at 15:00
  • Yeah, that's what I figured. I'll look into that. Giving no arguments to invalidate() broke the function, so I'll have to find another way for not showing the error messages on the fields. I could just give it an empty string as message, but I've styled the messages with pseudo-elements, so that would look even stranger.
    – tobiasg
    Sep 19, 2018 at 15:02
  • Looks like there is a hook called wpcf7_form_response_output, but some sources tell me that it isn't reliable when using AJAX with the form. So that leaves me with modifying the message with JS, but I don't really know how to let the JS script know that it's this particular validation that's triggering the error.
    – tobiasg
    Sep 19, 2018 at 15:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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