• Hello! I’m trying to check if a field is empty to add in a custom error message.

    Here is the strange part and I don’t know what I’m missing. For a test, I did a simple form.

    <label> Your name
        [text* your-name] </label>
    
    <label> Your message (optional)
        [textarea* your-message] </label>
    
    [submit "Submit"]

    If I check if your-name field is NOT empty, this code works great.

    add_filter('wpcf7_validate_text*', 'custom_text_validation_filter', 20, 2);
    
    function custom_text_validation_filter($result, $tag) {
    	
    	if ('your-name' == $tag->name) {
    		$field_name = isset( $_POST['your-name'] ) ? trim( $_POST['your-name'] ) : '';
    		
    		if ($field_name !== '') {
    			 $result->invalidate( $tag, "Are you sure this is the correct name?" );
    		}
    	}
    
        return $result;
    }

    As you see, I’m checking if field_name is NOT empty.
    BUT, if I change that to check if the field IS empty, the form just displays the generic “Please fill out this field text”.

    add_filter('wpcf7_validate_text*', 'custom_text_validation_filter', 20, 2);
    
    function custom_text_validation_filter($result, $tag) {
    	
    	if ('your-name' == $tag->name) {
    		$field_name = isset( $_POST['your-name'] ) ? trim( $_POST['your-name'] ) : '';
    		
    		if ($field_name == '') {
    			 $result->invalidate( $tag, "Are you sure this is the correct name?" );
    		}
    	}
    
        return $result;
    }

    I’m unsure why checking if its not empty is working correctly but changing it to check if it IS empty is not working.

    Anyone see what I could be doing wrong?

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

    (@takayukister)

    The current Contact Form 7 has two different user input validation mechanisms: Schema-Woven Validation and the traditional PHP filter-based custom validation that you use in the code. I guess the message you see if the field is empty is an output of SWV working on the client (browser) side. If you want to change the message, see Editing messages.

    Thread Starter grimesweb

    (@grimesweb)

    Good morning! Thank you for the reply. Here is my Dilemma. I do see in the messages tab in the, i can edit the validation messages for email, url, number.

    https://ibb.co/2MDpnzJ

    I also do see that i can set the custom validation messages for “all” text fields in the messages tab.

    https://ibb.co/qMhwYhX

    My issue is I just need to target the text field with the name “your-name” and no other text fields. The crazy part is my code works if I’m checking if the field is not empty. If I check if its not empty, add a character inside the your-name input field, and then hit submit, it gives me my custom error message.

    But if i change the one line to check if it IS empty, nothing happens and it reverts to the default “Please fill out this field”.

    Is there a different way I should be checking if the field is empty than just simply >

    if ($field_name == '')
    • This reply was modified 2 years, 7 months ago by grimesweb.
    Thread Starter grimesweb

    (@grimesweb)

    Just for the sake of clarity. This works >

    // Check if field is not empty and show custom message
    ($field_name !== '')

    https://ibb.co/c1B1gHz

    And just switching to this doesn’t.

    // Check if field is empty and show custom message
    ($field_name == '')

    https://ibb.co/5hW8Xkh

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Checking if field is empty not working’ is closed to new replies.