重力形式:日期验证失败
GRAVITY FORMS: Date Validation Failing
我在 WordPress 上的 functions.php 文件中设置了此过滤器,用于验证日期,因此取件日期时间早于 return 日期和时间。当日期和时间输入正确时,它应该通过验证,但它一直失败,我无法理解这一点。它只是突然停止工作。我只能指出这一点以将服务器上的 date/time 设置为格林威治时间。
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;
}
if (strpos($email['message'], 'Pickup Date') !== false) {
$email['message'] = explode('Pickup Date', $email['message']);
$email['message'][1] = str_replace('/', ', ', $email['message'][1]);
$email['message'][1] = str_replace('<, ', '</', $email['message'][1]);
$email['message'] = implode('Pickup Date', $email['message']);
}
$email['message'] = '<html>' . $email['message'] . '</html>';
return $email;
}, 10, 2 );
日期以这种格式输出。
string(22) "Pickup Date:25/06/2020"
string(28) "Pickup Time:25/06/2020 08:00"
string(22) "Return Date:26/06/2020"
string(28) "Return Time:26/06/2020 08:00"
谁能找出问题所在?
当您访问此 URL 时,您需要单击“需要报价”按钮。
您只需从验证条件中删除 =。
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;
}
我在 WordPress 上的 functions.php 文件中设置了此过滤器,用于验证日期,因此取件日期时间早于 return 日期和时间。当日期和时间输入正确时,它应该通过验证,但它一直失败,我无法理解这一点。它只是突然停止工作。我只能指出这一点以将服务器上的 date/time 设置为格林威治时间。
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;
}
if (strpos($email['message'], 'Pickup Date') !== false) {
$email['message'] = explode('Pickup Date', $email['message']);
$email['message'][1] = str_replace('/', ', ', $email['message'][1]);
$email['message'][1] = str_replace('<, ', '</', $email['message'][1]);
$email['message'] = implode('Pickup Date', $email['message']);
}
$email['message'] = '<html>' . $email['message'] . '</html>';
return $email;
}, 10, 2 );
日期以这种格式输出。
string(22) "Pickup Date:25/06/2020"
string(28) "Pickup Time:25/06/2020 08:00"
string(22) "Return Date:26/06/2020"
string(28) "Return Time:26/06/2020 08:00"
谁能找出问题所在?
当您访问此 URL 时,您需要单击“需要报价”按钮。
您只需从验证条件中删除 =。
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;
}