WordPress表单在单击提交按钮时未检查空值

时间:2019-07-27 03:47:54

标签: wordpress

我正在创建一个表单存在的插件。如下图所示:

enter image description here

现在,当我验证此表单时,消息字段的空值不会在第一次进行检查,而是从第二次!

此消息字段是使用wp_editor()函数创建的:

wp_editor( '', 'sn_message', array (            
    'editor_height' => 250,
    'drag_drop_upload' => true,
    'textarea_name' =>  'sn_message',
    'teeny' =>  true,
) );

Php代码以验证该表单字段:

$sn_to                  =   sanitize_text_field ( $_POST['sn_to'] );
$sn_subject             =   sanitize_text_field ( $_POST['sn_subject'] );           
$sn_multiple_email      =   isset( $_POST['sn_multiple_email']) ? $_POST['sn_multiple_email'] : '';
$sn_choose_option       =   isset( $_POST['sn_choose_option'] ) ? sanitize_text_field ( $_POST['sn_choose_option'] ) : '';
$sn_no_of_post          =   absint ( $_POST['sn_no_of_post'] );
$sn_message_before_body =   sanitize_textarea_field ( $_POST['sn_message_before_body'] );
$sn_message_after_body  =   sanitize_textarea_field ( $_POST['sn_message_after_body'] );
$sn_template            =   sanitize_text_field ( $_POST['sn_template'] );
$sn_message             =   sanitize_textarea_field ( $_POST['sn_message'] );
$sn_errors              =   array();

if( isset ( $sn_to, $sn_subject, $sn_message, $sn_choose_option ) ) {
    if( empty ( $sn_subject ) && empty ( $sn_to ) && empty ( $sn_multiple_email ) && empty ( $sn_message ) ) {
        $sn_errors[] = __( 'All fields are required', 'simple-newsletter' );
    } else {
        if( !empty ( $sn_to ) ) {
            if ( !is_email ( $sn_to ) ) {
                $sn_errors[] = __( 'Email address is incorrect', 'simple-newsletter' );
            }
        }
        if ( $sn_multiple_email ) {
            foreach ( $sn_multiple_email as $sn_email ) {
                if( !is_email( $sn_email ) ) {
                    $sn_errors[] = __( 'One of your multiple emails is incorrect', 'simple-newsletter' );
                }
            }
        }
        if( empty( $sn_to ) && empty ( $sn_multiple_email ) ) {
            $sn_errors[] = __( 'Write email address or choose from the multiple', 'simple-newsletter' );
        }
        if( empty ( $sn_subject ) ) {
            $sn_errors[] = __( 'Subject is required', 'simple-newsletter' );
        } elseif ( strlen ( $sn_subject ) > 150 ) {
            $sn_errors[] = __( 'Subject must be less 150 characters long', 'simple-newsletter' );
        }
        if( empty ( $sn_choose_option ) ) {
            $sn_errors[] = __( 'Choose what do you want to send', 'simple-newsletter' );
        } else {
            if ( $sn_choose_option == 'post' ) {
                if ( empty( $sn_no_of_post ) ) {
                    $sn_errors[] = __( 'Chooose how many posts you want to send', 'simple-newsletter' );
                } 
                if( empty( $sn_template ) ) {
                    $sn_errors[] = __( 'Choose a template for the newsletter email', 'simple-newsletter' );
                }
            } elseif ( $sn_choose_option == 'custom-message' ) {
                if( empty ( $sn_message ) ) {
                    $sn_errors[] = __( 'Message is required', 'simple-newsletter' );
                }
            }
        }           
    }

    if( !empty ( $sn_errors ) ) {
        echo "<div class='sn_error_message'>";
            foreach ( $sn_errors as $sn_error ) {
                echo "<p>$sn_error</p>";
            }
        echo "</div>";
    } else {                    

        if ( empty( $sn_multiple_email ) ) {
            $sn_to              =   $sn_to;
        } else {
            array_push ( $sn_multiple_email, $sn_to );
            $sn_multiple_email = array_unique( $sn_multiple_email );
            $sn_to              =   implode( ',' , $sn_multiple_email );
        }

        // Send email
        $this->display->sn_send_email( $sn_to, $sn_subject, $sn_choose_option, $sn_no_of_post, $sn_message_before_body, $sn_template, $sn_message_after_body, $sn_message );
    }
}

您可以看到此行吗?

if( empty ( $sn_message ) ) {
    $sn_errors[] = __( 'Message is required', 'simple-newsletter' );
}

这是说如果消息字段为空,则显示错误消息。但这不是第一次/单击不会显示任何错误消息,而是从第二次/单击开始一直显示。.

有人可以告诉我为什么吗?

0 个答案:

没有答案