• Resolved craigstanfield

    (@craigstanfield)


    I’m intercepting the wpcf7_before_send_mail during this I make a call to an api, I need to invalidate the form if the api returns an error then i need to invalidate the request and return the error passed back from the api call.

    I set a flag to false on api failure and the error message is also stored but my form is going through as success despite the failure I induce.

    add_action("wpcf7_before_send_mail", "wpcf7_send_contact_builder");
    function wpcf7_send_contact_builder($form) {
        $submission = WPCF7_Submission::get_instance();
        $wpcf7_data = $submission->get_posted_data();
        ... api call and set $success to true if ok and false if not ...
        if (!$success) {
            $form->status = 'validation_failed (statuscode:' . $xml->status->statuscode[0] . ').';
            $form->valid = false;
            $form->response = $xml->status->statusdesc[0];
            return $forml
        }
    }

    I’ve also tried using

    $form->invalidate('validation_failed (statuscode:' . $xml->status->statuscode[0] . ').', $xml->status->statusdesc[0]);

    But whichever way i am unable to prevent the success email being sent and the form validates as successful. debugging proved that the !success in the if statement is working and the code contained is added to the variable. I also tried as if form was an array ($form[‘valid’] = false) but this also didnt work and the form submits as successful. Any ideas of what I’m missing here? I’ve omitted the code for the api call itself and the determining of the correct form id, both of these work correctly, only the form I’m after is parsed and the api call is returning the expected data

    https://www.ads-software.com/plugins/contact-form-7/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Takayuki Miyoshi

    (@takayukister)

    Here is a tip for custom validation: Developer’s Cookbook – Custom Validation

    Thread Starter craigstanfield

    (@craigstanfield)

    Hi Takayuki,

    Ok interesting, but i wish to call the api after the form has been validated, that way the data is ok to begin with, mainly the api will fail if the site is down which means data is not backed up.

    Using the method on the link you supplied i was able to invalidate the form and show the api result beneath the email field, in my case there is no field for the api. I tried to create a custom type called api but hit issues, i also tried with submit validation which executed but failed to invalidate the form.

    So is there any way i can validate the form fields as normal but then if the data sent to the api fails we invalidate the form.

    Thread Starter craigstanfield

    (@craigstanfield)

    Thanks Takayuki, after revisiting the link i worked out how to add a custom type of api which fixed my issue

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘invalidate after api call’ is closed to new replies.