字段错误后gform动态填充不起作用?

时间:2017-05-16 09:04:35

标签: php jquery wordpress

我的函数中有这段代码。

add_filter('gform_pre_validation_13', 'populate_departure_date');
add_filter('gform_pre_submission_filter_13', 'populate_departure_date');
add_filter('gform_admin_pre_render_13', 'populate_departure_date');
add_filter('gform_pre_render_13', 'populate_departure_date');
function populate_departure_date($form)
{
    $today = strtotime(date('Y-m-d'));
    foreach ($form['fields'] as &$field) {
        if ($field->id == 37) {
        $pid = $_POST['post_id'];
        session_start();
        if (!isset($_SESSION['post_id'])) {
            $_SESSION['post_id'] = $pid;
        }

        if (have_rows('small_group_journey', $pid)):
            while (have_rows('small_group_journey', $pid)):the_row();
                if (!empty(get_sub_field('start_date/end_date'))) {

                    $dateS = explode('-', get_sub_field('start_date/end_date'));
                    if (strtotime($dateS[0]) >= $today) {
                        $choices[] = array('text' => date('F j Y', strtotime($dateS[0])) . ' - ' . date('F j Y', strtotime($dateS[1])), 'value' => date('F j Y', strtotime($dateS[0])) . ' - ' . date('F j Y', strtotime($dateS[1])));
                    }
                }
            endwhile;
        endif;

        $field->placeholder = 'Fixed Departure Dates';

        $field->choices = $choices;
    }
}

return $form;
}

我在预订表单页面中使用此短代码。

do_shortcode('[gravityform id="2" title="false" description="true" ajax="false" field_values="trip_name=' . $trip_title . '&departure_date=' . $_POST['start_date'] . '-' . $_POST['end_date'] . '" tabindex="23" ]');

所有事情最初都在起作用

enter image description here

但是,在我点击提交并且有验证错误后,所有字段都会消失。

enter image description here

我尝试过使用会话变量保留,但我只能保留页面标题。 任何解决方案

1 个答案:

答案 0 :(得分:0)

我忘记使用一个会话变量,该变量在表单加载时存储数据,如果打开任何其他页面则将其销毁。现在看起来一切都很好。

function populate_departure_date($form)
{
$today = strtotime(date('Y-m-d'));
foreach ($form['fields'] as &$field) {
    if ($field->id == 37) {
    $pid = $_POST['post_id'];
    session_start();
    if (!isset($_SESSION['post_id'])) {
        $_SESSION['post_id'] = $pid;
    }

    if (have_rows('small_group_journey', $_SESSION['post_id'])):
        while (have_rows('small_group_journey', $_SESSION['post_id'])):the_row();
            if (!empty(get_sub_field('start_date/end_date'))) {

                $dateS = explode('-', get_sub_field('start_date/end_date'));
                if (strtotime($dateS[0]) >= $today) {
                    $choices[] = array('text' => date('F j Y', strtotime($dateS[0])) . ' - ' . date('F j Y', strtotime($dateS[1])), 'value' => date('F j Y', strtotime($dateS[0])) . ' - ' . date('F j Y', strtotime($dateS[1])));
                }
            }
        endwhile;
    endif;

    $field->placeholder = 'Fixed Departure Dates';

    $field->choices = $choices;
}
}

return $form;
}