Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom validation is not working in Contact Form 7 in ver 4.1.1

I have to make a form with custom validation field in contact form 7. It is not working with latest version (4.1.1) of Contact Form 7 but working in older version.

I have created a field for getting coupon code from the form. I want to validate the entry if the coupon is started from "HIP". My code is given below:

add_filter( 'wpcf7_validate_text', 'your_validation_filter_func', 999, 2 );
add_filter( 'wpcf7_validate_text*', 'your_validation_filter_func', 999, 2 );


function your_validation_filter_func( $result, $tag ) {
$type = $tag['type'];
$name = $tag['name'];      
if ( 'coupon_code' == $name ) {
$the_value = $_POST[$name];

        $myresult = substr($the_value, 0, 3);
        if($myresult=="HIP")
        {
            $result['valid'] = true;
        }
        else
        {
            $result['valid'] = false;
            $result['reason'][$name] = "Not a valid coupon code";
        }
}

return $result;
}

Give me suggestion please.

like image 202
Jitendra Damor Avatar asked Apr 15 '15 05:04

Jitendra Damor


People also ask

How do I create a custom validation in Contact Form 7?

In Contact Form 7, a user-input validation is implemented as a filter function. The filter hook used for the validation varies depending on the type of form-tag and is determined as: wpcf7_validate_ + {type of the form-tag}. So, for text form-tags, the filter hook wpcf7_validate_text is used.

How do I change the error in Contact Form 7?

You can change the text used in each type of error message. For example, the default message for required fields is “The field is required.” To change this message, edit the text in the Messages tab panel.

How do I use custom validation in contact form 7?

From contactform7.com on Custom Validation → Validation as a Filter: In Contact Form 7, a user-input validation is implemented as a filter function. The filter hook used for the validation varies depending on the type of form-tag and is determined as: wpcf7_validate_ + {type of the form-tag}.

Does contact form 7 support email confirmation fields?

Contact Form 7 doesn’t support email confirmation fields by default (Because I think it’s ridiculous. Who wants to type the same address twice? People know copy and paste). Still, it can be a good example of custom validation. In Contact Form 7, a user-input validation is implemented as a filter function.

What are some good examples of custom validation in WordPress?

Still, it can be a good example of custom validation. In Contact Form 7, a user-input validation is implemented as a filter function. The filter hook used for the validation varies depending on the type of form-tag and is determined as: wpcf7_validate_ + {type of the form-tag}.

Is it the validator’s bug if it detects configuration errors?

The validation doesn’t affect behavior of a contact form at all, even if it detects configuration errors. My contact form works regardless of configuration errors. Is it the validator’s bug? # My contact form works regardless of configuration errors.


2 Answers

I had a similar issue with contact form 7 custom validations. Finally landed on this post and also on official custom form 7 custom validations link here: http://contactform7.com/2015/03/28/custom-validation/.

The only update required for the code working on the earlier versions of CF7 is to replace the following line of code:

$result['reason'][$name] = 'Your custom validation message goes here';

with:

$result->invalidate( $tag, "Your custom validation message goes here." );
like image 136
Ruchika Avatar answered Sep 30 '22 14:09

Ruchika


I have also this issue was occurred when i update the contact form 7 with 4.1.1. In the latest version of contact form 7 old custom validation code is not working.

So after very research i have found solution for this. So in your case you need to change in your code as following. May be it will be helpful for you.

add_filter('wpcf7_validate_text', 'your_validation_filter_func', 999, 2);
add_filter('wpcf7_validate_text*', 'your_validation_filter_func', 999, 2);

function your_validation_filter_func($result, $tag) {
$type = $tag['type'];
$name = $tag['name'];

if ('coupon_code' == $name) {

    $the_value = $_POST[$name];

    $myresult = substr($the_value, 0, 3);
    if ($myresult == "HIP") {
        $result['valid'] = true;
    } else {
        $result->invalidate($tag, wpcf7_get_message('invalid_coupon_code'));
    }
}

return $result;
}

add_filter('wpcf7_messages', 'mywpcf7_text_messages');

function mywpcf7_text_messages($messages) {
return array_merge($messages, array(
    'invalid_coupon_code' => array(
        'description' => __("Coupon is invalid", 'contact-form-7'),
        'default' => __('Coupon seems invalid.', 'contact-form-7')
)));
}
like image 43
Chandresh Avatar answered Sep 30 '22 12:09

Chandresh