简体   繁体   English

symfony bootstrap-datepicker-保留默认日期

[英]symfony bootstrap-datepicker - default date persisted

I have a problem with this datepicker : http://bootstrap-datepicker.readthedocs.org/en/stable/index.html 我对此日期选择器有疑问: http : //bootstrap-datepicker.readthedocs.org/en/stable/index.html

I work with symfony framework, on last version. 我使用最新版本的symfony框架。

I do all css/js file inclusion, i add a class on my field for js, and i use this configuration : 我将所有css / js文件都包含在内,我在js字段中添加了一个类,然后使用此配置:

$('.datepick').datepicker({
        language: "fr",
        todayBtn: "linked",
        keyboardNavigation: false,
        forceParse: false,
        calendarWeeks: true,
        autoclose: true
    });

So in regular step, i can select a date with my datepicker. 因此,在常规步骤中,我可以使用日期选择器选择一个日期。

My problem is : when i don't select a date in form (optionnal field), the current date is always saved on field. 我的问题是:当我不在表单(可选字段)中选择日期时,当前日期始终保存在字段中。

I just want to remove this behaviour : when no date is selected, no date is persist in entity. 我只想删除此行为:当未选择任何日期时,实体中不会保留任何日期。 I don't have any idea for solve this. 我不知道要解决这个问题。

Anyone have an idea ? 有人有主意吗?

-- -

Thx for you help 谢谢你的帮助

my field in formtype is : 我的表单中的字段是:

$builder->add('dateNaissance', 'birthday', array(
            'label' => 'form.tiers.dateNaissance', 'translation_domain' => 'GBPCoreBundle', 'required' => false,
            'widget' => 'single_text', 'format' => 'dd/MM/yyyy', 'input' => 'datetime', 'attr' => array('class' => 'datepick')
        ))

My view is : 我的看法是:

{{ form_row(form.dateNaissance) }}

My entity is : 我的实体是:

/**
 * @var \Date
 *
 * @ORM\Column(name="dateNaissance", type="date", nullable=true)
 * @Assert\Date(message="La date de naissance {{ value }} doit être un type {{ type }} valide.")
 */
private $dateNaissance;

and my consctructor is empty, i didn't set default value 而且我的构造器是空的,我没有设置默认值

My controller is very basic, i don't act on this field 我的控制器是非常基本的,我不在这方面采取行动

First, verify your entity date field have nullable option like nullable=true 首先,验证您的实体日期字段具有nullable选项,例如nullable=true

Then, If the problem persists, and you can't find any option to do this trick, you can use Javascript. 然后,如果问题仍然存在,并且找不到解决此问题的方法,则可以使用Javascript。

On document load, set the datepicker value with an empty string, using jQuery it's like : 在文档加载时,使用一个空字符串设置datepicker值,使用jQuery就像这样:

$(document).ready(function(){
    $('#dateField').val('');
});

Now, the default value is empty, and if value change (user choose a date), you save the date, else you save NULL or '' 现在,默认值为空,如果值更改(用户选择日期),则保存日期,否则保存NULL''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM