Symfony2生日字段Jquery自动完成

时间:2016-04-26 01:12:06

标签: php jquery symfony dom autocomplete

我一直在尝试使用Jquery Autocomplete填充人员表单,我无法弄清楚如何访问生日字段的dom元素。

/ * FORM * /

$builder->add('dob', 'birthday', array(
        'label' => 'Date of Birth',
        'placeholder' => array(
            'year' => 'Year', 'month' => 'Month', 'day' => 'Day',
        ),
        'widget' => 'choice',
        'format' => 'dd MM yyyy',
        'required' => true,
        'constraints' => array(
            new NotBlank(array(
                'message' => 'Please add your birthday'
                    ))
        )
    ));

/ * JS * /

function fillPerson(form, person) {
    form.find('.id').val(person.id);
    form.find('.first-name').val(person.first_name);
    form.find('.middle-name').val(person.middle_name);
    form.find('.last-name').val(person.last_name);
    console.log('logging persons DOB');
    console.log(person);
    form.find('.dob').val(person.dob);
    return true;
}

/ * twig template * /

enter image description here

当我将生日字段用作“选择”窗口小部件时,我需要能够从我的js person对象中设置每个选项列表。

1 个答案:

答案 0 :(得分:0)

选项1:使用single_text小部件:

use Symfony\Component\Form\Extension\Core\Type\DateType;
// ...

$builder->add('date_created', DateType::class, array(
    'widget' => 'single_text',
    // this is actually the default format for single_text
    'format' => 'yyyy-MM-dd',
));

http://symfony.com/doc/current/reference/forms/types/date.html#format

选项2:分割日期:

var birthdate = person.dob.split(' ');
form.find('[name="director[person][dob][day]"').val(birthdate[0]);
form.find('[name="director[person][dob][month]"').val(birthdate[1]);
form.find('[name="director[person][dob][year]"').val(birthdate[2]);

(未经测试)