Forums

[Plugin: Contact Form 7] erase fields onFocus > validation problems (7 posts)

  1. Soundofsirens
    Member
    Posted 2 years ago #

    Hi.
    For design reasons i have to put the field descriptions (Your Name, Your Mail etc.) inside the fields with values.
    Then i included some JS into the plugin.php where the html output is generated, to kill the value on focus. To that point – everything works fine.

    But now validation for standard required textfields does not work properly since it only checks if there's an value. And because there are values by default – no error message pops out. Know what i mean?

    Can someone provide me with the code to fix that?
    It's unfortunately far beyond my php knowledge.

    :)

    Btw. This would be a very nice feature for future versions. I think many people would enjoy to have that option.

    Thanks and bests,
    m.

  2. Soundofsirens
    Member
    Posted 2 years ago #

    Nobody ever had the same idea? Really? :o

  3. AdamKayce
    Member
    Posted 2 years ago #

    I'm wanting to get the fields to clear onFocus as well... is there a simple way to make this happen?

  4. shrenno
    Member
    Posted 2 years ago #

    I edited \modules\text.php

    and replaced

    $html = '<input type="text" name="' . $name . '" value="' . esc_attr( $value ) . '"' . $atts . ' />';

    with

    $html = '<input type="text" name="' . $name . '" value="' . esc_attr( $value ) . '"' . $atts . ' onfocus="if(this.value==\'';
    
    $html = $html . esc_attr( $value ) . '\') this.value=\'\';" onblur="if(this.value==\'\') this.value=\'' . esc_attr( $value ) . '\';" />';

    You'll have to do something similar for the other fields (textarea, etc.).

    What I am still trying to fix is validating these fields.
    Now that there they have default text in them, and if they are "required" fields, that default text lets them pass validation.

  5. piticstyle
    Member
    Posted 1 year ago #

    10x shrenno!

  6. vladvornicu
    Member
    Posted 1 year ago #

    Hi,

    Go to modules/text.php line 110 and add after $name = $tag['name'] the following lines :

    $values = (array) $tag['values'];
    
        // Value
        if ( is_a( $wpcf7_contact_form, 'WPCF7_ContactForm' ) && $wpcf7_contact_form->is_posted() ) {
            if ( isset( $_POST['_wpcf7_mail_sent'] ) && $_POST['_wpcf7_mail_sent']['ok'] )
                $value = '';
            else
                $value = stripslashes_deep( $_POST[$name] );
        } else {
            $value = isset( $values[0] ) ? $values[0] : '';
        }
    
        if($_POST[$name] == $values[0]) $_POST[$name]= '';

    Good luck.

  7. mrittman
    Member
    Posted 1 year ago #

    Thanks vladvornicu. Now why can't the developers of Contact Form 7 add those lines of code to the plugin? The only problem with doing it ourselves is that we lose this on upgrades...

Topic Closed

This topic has been closed to new replies.

About this Topic