function wpcf7_text_custom_validation_message($result, $tag)
{
    $cmtagobj = new WPCF7_Shortcode($tag);
    $post_id = sanitize_text_field($_POST['_wpcf7']);
    $name = $cmtagobj->name;
    $key = "_cf7cm_" . $name;
    $val = get_post_meta($post_id, $key, true);
    $enable = get_post_meta($post_id, '_cf7cm_enable_errors');
    if ($enable[0] != 0) {
        $value = isset($_POST[$name]) ? trim(wp_unslash(strtr((string) $_POST[$name], "\n", " "))) : '';
        if ('text' == $cmtagobj->basetype) {
            if ($cmtagobj->is_required() && '' == $value) {
                $result->invalidate($cmtagobj, $val);
            }
        }
        if ('email' == $cmtagobj->basetype) {
            if ($cmtagobj->is_required() && '' == $value) {
                $result->invalidate($cmtagobj, $val);
            } elseif ('' != $value && !wpcf7_is_email($value)) {
                $key = "_cf7cm_" . $name . "-valid";
                $val = get_post_meta($post_id, $key, true);
                if ($val) {
                    $result->invalidate($cmtagobj, $val);
                } else {
                    $result->invalidate($cmtagobj, wpcf7_get_message('invalid_email'));
                }
            }
        }
        if ('url' == $cmtagobj->basetype) {
            if ($cmtagobj->is_required() && '' == $value) {
                $result->invalidate($cmtagobj, $val);
            } elseif ('' != $value && !wpcf7_is_url($value)) {
                $result->invalidate($cmtagobj, wpcf7_get_message('invalid_url'));
            }
        }
        if ('tel' == $cmtagobj->basetype) {
            if ($cmtagobj->is_required() && '' == $value) {
                $result->invalidate($cmtagobj, $val);
            } elseif ('' != $value && !wpcf7_is_tel($value)) {
                $result->invalidate($cmtagobj, wpcf7_get_message('invalid_tel'));
            }
        }
        if (!empty($value)) {
            $maxlength = $cmtagobj->get_maxlength_option();
            $minlength = $cmtagobj->get_minlength_option();
            if ($maxlength && $minlength && $maxlength < $minlength) {
                $maxlength = $minlength = null;
            }
            $code_units = wpcf7_count_code_units($value);
            if (false !== $code_units) {
                if ($maxlength && $maxlength < $code_units) {
                    $result->invalidate($cmtagobj, wpcf7_get_message('invalid_too_long'));
                } elseif ($minlength && $code_units < $minlength) {
                    $result->invalidate($cmtagobj, wpcf7_get_message('invalid_too_short'));
                }
            }
        }
    }
    return $result;
}
Esempio n. 2
0
function wpcf7_text_validation_filter($result, $tag)
{
    $tag = new WPCF7_Shortcode($tag);
    $name = $tag->name;
    $value = isset($_POST[$name]) ? trim(wp_unslash(strtr((string) $_POST[$name], "\n", " "))) : '';
    if ('text*' == $tag->type) {
        if ('' == $value) {
            $result['valid'] = false;
            $result['reason'][$name] = wpcf7_get_message('invalid_required');
        }
    }
    if ('email' == $tag->basetype) {
        if ($tag->is_required() && '' == $value) {
            $result['valid'] = false;
            $result['reason'][$name] = wpcf7_get_message('invalid_required');
        } elseif ('' != $value && !wpcf7_is_email($value)) {
            $result['valid'] = false;
            $result['reason'][$name] = wpcf7_get_message('invalid_email');
        }
    }
    if ('url' == $tag->basetype) {
        if ($tag->is_required() && '' == $value) {
            $result['valid'] = false;
            $result['reason'][$name] = wpcf7_get_message('invalid_required');
        } elseif ('' != $value && !wpcf7_is_url($value)) {
            $result['valid'] = false;
            $result['reason'][$name] = wpcf7_get_message('invalid_url');
        }
    }
    if ('tel' == $tag->basetype) {
        if ($tag->is_required() && '' == $value) {
            $result['valid'] = false;
            $result['reason'][$name] = wpcf7_get_message('invalid_required');
        } elseif ('' != $value && !wpcf7_is_tel($value)) {
            $result['valid'] = false;
            $result['reason'][$name] = wpcf7_get_message('invalid_tel');
        }
    }
    if (isset($result['reason'][$name]) && ($id = $tag->get_id_option())) {
        $result['idref'][$name] = $id;
    }
    return $result;
}
Esempio n. 3
0
function wpcf7_text_validation_filter($result, $tag)
{
    $tag = new WPCF7_Shortcode($tag);
    $name = $tag->name;
    $value = isset($_POST[$name]) ? trim(wp_unslash(strtr((string) $_POST[$name], "\n", " "))) : '';
    if ('text' == $tag->basetype) {
        if ($tag->is_required() && '' == $value) {
            $result->invalidate($tag, wpcf7_get_message('invalid_required'));
        }
    }
    if ('email' == $tag->basetype) {
        if ($tag->is_required() && '' == $value) {
            $result->invalidate($tag, wpcf7_get_message('invalid_required'));
        } elseif ('' != $value && !wpcf7_is_email($value)) {
            $result->invalidate($tag, wpcf7_get_message('invalid_email'));
        }
    }
    if ('url' == $tag->basetype) {
        if ($tag->is_required() && '' == $value) {
            $result->invalidate($tag, wpcf7_get_message('invalid_required'));
        } elseif ('' != $value && !wpcf7_is_url($value)) {
            $result->invalidate($tag, wpcf7_get_message('invalid_url'));
        }
    }
    if ('tel' == $tag->basetype) {
        if ($tag->is_required() && '' == $value) {
            $result->invalidate($tag, wpcf7_get_message('invalid_required'));
        } elseif ('' != $value && !wpcf7_is_tel($value)) {
            $result->invalidate($tag, wpcf7_get_message('invalid_tel'));
        }
    }
    if (!empty($value)) {
        $maxlength = $tag->get_maxlength_option();
        $minlength = $tag->get_minlength_option();
        if ($maxlength && $minlength && $maxlength < $minlength) {
            $maxlength = $minlength = null;
        }
        $code_units = wpcf7_count_code_units($value);
        if (false !== $code_units) {
            if ($maxlength && $maxlength < $code_units) {
                $result->invalidate($tag, wpcf7_get_message('invalid_too_long'));
            } elseif ($minlength && $code_units < $minlength) {
                $result->invalidate($tag, wpcf7_get_message('invalid_too_short'));
            }
        }
    }
    return $result;
}