评论表单浏览器验证

时间:2018-04-29 19:04:20

标签: validation comments placeholder

对于我的Wordpress评论表单,我使用“浏览器验证”,将以下代码添加到我的functions.php中:

/**
 * Enable form validation
 */
function custom_enable_comment_form_validation() {
    if ( comments_open() && current_theme_supports( 'html5' ) ) {
        echo '<script>document.getElementById("commentform").removeAttribute("novalidate");</script>' . PHP_EOL;
    }
}
add_action( 'wp_footer', 'custom_enable_comment_form_validation' );

我的评论表单包含姓名和电子邮件字段+评论文本字段。各个领域都需要。不幸的是,“必填字段”消息只出现在评论字段中......当我填写评论字段并将名称和电子邮件字段留空时,Wordpress仍然会将用户重定向到错误页面。

An example of my comment form with a 'required field' message which need to pop-up if a field is blank. Unfortunately only the comment field is showing the message. 我的评论表单字段也使用占位符,当我删除名称和电子邮件字段的占位符时,我发现所有字段都正常工作并显示弹出消息。所以这个bug很可能是由以下代码引起的:

<?php
/* Using an placeholder in the name and email field. */
function my_update_comment_fields( $fields ) {

    $commenter = wp_get_current_commenter();
    $req       = get_option( 'require_name_email' );
    $label     = $req ? '*' : ' ' . __( '(optional)', 'text-domain' );
    $aria_req  = $req ? "aria-required='true'" : '';

    $fields['author'] =
        '<p class="comment-form-author">
            <input id="author" name="author" type="text" placeholder="' . esc_attr__( "Your name", "text-domain" ) . '" value="' . esc_attr( $commenter['comment_author'] ) .
        '" size="30" ' . $aria_req . ' />
        </p>';

    $fields['email'] =
        '<p class="comment-form-email">
            <input id="email" name="email" type="email" placeholder="' . esc_attr__( "Your email address", "text-domain" ) . '" value="' . esc_attr( $commenter['comment_author_email'] ) .
        '" size="30" ' . $aria_req . ' />
        </p>';

    return $fields;
}
add_filter( 'comment_form_default_fields', 'my_update_comment_fields' );
?>

不幸的是我找不到它出错的地方......有人可以解决问题吗?

有人还可以告诉我如何将弹出消息的文本更改为functions.php中的默认消息吗?

提前致谢。

0 个答案:

没有答案
相关问题