symfony表单中生日的/ DateTime字段

时间:2022-06-16 07:17:49

I need to retrieve the birthday of a student from the database and set it as the birthday of a student entity. Then I need to load that data to a form.

我需要从数据库中检索学生的生日并将其设置为学生实体的生日。然后我需要将该数据加载到表单。

$birth_day = strtotime($student_[0]['birthday']);
$birth__day = date('Y-m-d H:i:s',$birth_day); 
$student->setBirthday($birth__day);

And in the form builder I have used the below code

在表单构建器中,我使用了以下代码

add('birthday', 'birthday',array(
            'data' => new \DateTime(),'placeholder'=>'-SELECT-'))

Instead of the birthday that particular student, it shows the today's date.

它显示了今天的日期,而不是特定学生的生日。

Can someone please explain what should I do to load the birthday instead of the today's date. Thankyou in advance.

有人可以解释我应该怎么做加载生日而不是今天的日期。先谢谢你。

1 个解决方案

#1


1  

When you use the 'data' option, you are setting the default value for the field so in your example you are setting it to a new Datetime (today).

当您使用'data'选项时,您正在设置字段的默认值,因此在您的示例中,您将其设置为新的Datetime(今天)。

Here is the symfony doc page about it http://symfony.com/doc/current/reference/forms/types/form.html#data

这是关于它的symfony doc页面http://symfony.com/doc/current/reference/forms/types/form.html#data

Like @abdiel suggested, remove the data part.

像@abdiel建议的那样,删除数据部分。

#1


1  

When you use the 'data' option, you are setting the default value for the field so in your example you are setting it to a new Datetime (today).

当您使用'data'选项时,您正在设置字段的默认值,因此在您的示例中,您将其设置为新的Datetime(今天)。

Here is the symfony doc page about it http://symfony.com/doc/current/reference/forms/types/form.html#data

这是关于它的symfony doc页面http://symfony.com/doc/current/reference/forms/types/form.html#data

Like @abdiel suggested, remove the data part.

像@abdiel建议的那样,删除数据部分。