重力形式:日期验证失败

我在 WordPress 上的 Functions.php 文件中设置了此过滤器,用于验证日期,因此取货日期时间早于返回日期和时间。当日期和时间输入正确时,它应该通过验证,但它一直失败,我只是无法理解这一点。它突然停止工作了。我只能查明这一点以将服务器上的日期/时间设置为格林威治时间。


add_filter( 'gform_validation', 'custom_validation' );

function custom_validation( $validation_result ) {


    $form = $validation_result['form'];

    $pickupDate = null;

    $returnDate = null;


    foreach( $form['fields'] as & $field ) {

        if ( $field->label == 'Pickup Date' ) {

            $pickupDate = rgpost('input_' . $field->id);

            //var_dump("Pickup Date:" . $pickupDate);

        }


        if ( $field->label == 'Pickup Time' ) {

            $pickupDate .= ' ' . rgpost('input_' . $field->id);

            //var_dump("Pickup Time:" . $pickupDate);

        }


        if ( $field->label == 'Return Date' ) {

            $returnDate = rgpost('input_' . $field->id);

            //var_dump("Return Date:" . $returnDate);

        }


        if ( $field->label == 'Return Time' ) {

            $returnDate .= ' ' . rgpost('input_' . $field->id);

            //var_dump("Return Time:" . $returnDate);


            if (strtotime($returnDate) <= strtotime($pickupDate)) {


                //var_dump($returnDate);

                //var_dump($pickupDate);


                $validation_result['is_valid'] = false;

                $field->failed_validation = true;

                $field->validation_message = 'Please ensure your selected Pickup Date and time is earlier than your Return Date and Time';

                break;

            }

        }

    }


    //Assign modified $form object back to the validation result

    $validation_result['form'] = $form;


    return $validation_result; 

}


add_filter( 'gform_pre_send_email', function ( $email, $message_format ) {

    if ( $message_format != 'html' ) {

        return $email;

    }



任何人都可以找出问题出在哪里吗?


当您访问此URL时,您需要单击&ldquo;需要报价&rdquo;按钮。



回首忆惘然
浏览 157回答 1
1回答

慕哥9229398

您只需从验证条件中删除 = 即可。add_filter( 'gform_validation', 'custom_validation' );function custom_validation( $validation_result ) {$form = $validation_result['form'];$pickupDate = null;$returnDate = null;foreach( $form['fields'] as & $field ) {&nbsp; &nbsp; if ( $field->label == 'Pickup Date' ) {&nbsp; &nbsp; &nbsp; &nbsp; $pickupDate = rgpost('input_' . $field->id);&nbsp; &nbsp; &nbsp; &nbsp; //var_dump("Pickup Date:" . $pickupDate);&nbsp; &nbsp; }&nbsp; &nbsp; if ( $field->label == 'Pickup Time' ) {&nbsp; &nbsp; &nbsp; &nbsp; $pickupDate .= ' ' . rgpost('input_' . $field->id);&nbsp; &nbsp; &nbsp; &nbsp; //var_dump("Pickup Time:" . $pickupDate);&nbsp; &nbsp; }&nbsp; &nbsp; if ( $field->label == 'Return Date' ) {&nbsp; &nbsp; &nbsp; &nbsp; $returnDate = rgpost('input_' . $field->id);&nbsp; &nbsp; &nbsp; &nbsp; //var_dump("Return Date:" . $returnDate);&nbsp; &nbsp; }&nbsp; &nbsp; if ( $field->label == 'Return Time' ) {&nbsp; &nbsp; &nbsp; &nbsp; $returnDate .= ' ' . rgpost('input_' . $field->id);&nbsp; &nbsp; &nbsp; &nbsp; //var_dump("Return Time:" . $returnDate);&nbsp; &nbsp; &nbsp; &nbsp; if (strtotime($returnDate) < strtotime($pickupDate)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //var_dump($returnDate);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //var_dump($pickupDate);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $validation_result['is_valid'] = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $field->failed_validation = true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $field->validation_message = 'Please ensure your selected Pickup Date and time is earlier than your Return Date and Time';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}//Assign modified $form object back to the validation result$validation_result['form'] = $form;return $validation_result;&nbsp;}
打开App,查看更多内容
随时随地看视频慕课网APP