1

Here is the situation: I have to make a textarea a required field when the user selects "Other" in a dropdown. That does work, however whenever I try to chec if the validation on all other fields BEFORE works... well it doesn't .. It doesn't say the user should fill it up, although its mostly required fields. It does work when I add remove_filter("blabla", "blabla", 10, 2); but then the custom validation does not work. I hope you guys have an idea where this might be coming from ... that's my code

add_filter("wpcf7_validate_textarea", 'custom_text_validation_filter', 10, 2);
function custom_text_validation_filter ( $result, $tag )
{
    $result = new WPCF7_Validation();

    if ( $tag->name != "comment" )
    {
        return $result;
    }
    else
    {
        $reclReason = isset( $_POST['reason']) ? $_POST['reason'] : ' ';
        $comment = isset($_POST['comment']) ? $_POST['comment'] : ' ';

        if ( $reclReason == 'Other*' && $comment == '' )
        {
            $result->invalidate($tag->name, "Please fill in the required field!");
        }
        else
        {
            $result['valid'] = true;
        }
    }
    return $result;
}

also without the else at the end (making the field valid otherwise) it also doesn't work, it says it's invalid although there has been something written in..

  • Did you check all the $_POST values (reason and comment) for the expected values? Also you set $comment as space and check if it it's empty. – moped Dec 10 '19 at 15:58
  • @moped yes, they are fine. Hm, you're right. The client changed their decision and would just like to have a "info" text under the textarea when "other" is chosen. I made that with JavaScript and it worked. So if someone else is having the same issue like me, maybe they find their solution here :) Still that doesn't answer why the validation on all other fields doesn't work.. – Roksana Dec 11 '19 at 13:11

Your Answer

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

Browse other questions tagged or ask your own question.