表格中的可选日期
Optional date in form
想要以 symfony 形式添加可选的 DateType class。它有点管用,因为我可以在不设置日期的情况下提交表单,但它会自动设置今天的日期。
TodoType.php
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('name')
->add('Deadline', DateType::class, [
'widget' => 'single_text',
'required' => false,
'empty_data' => ''
])
->add('Submit', SubmitType::class)
;
}
截止实体
/**
* @ORM\Column(type="date", nullable=true)
*/
private $deadline;
...
public function getDeadline(): ?\DateTimeInterface
{
return $this->deadline;
}
public function setDeadline(\DateTimeInterface $deadline = null): self
{
$this->deadline = $deadline;
return $this;
}
TodoController.php
/**
* @Route("/todos", methods={"GET", "POST"}, name="todos")
*
*/
public function todos(EntityManagerInterface $entityManager, Request $request): Response
{
// Rendering todos
$todos = $entityManager->getRepository(Todo::class)
->findBy(
['owner' => $this->getUser()]
);
// Creating new TODO
$todo = new Todo();
$todo
->setOwner($this->getUser())
->setCreationDate(new \DateTime());
$form = $this->createForm(TodoType::class, $todo);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$entityManager->persist($todo);
$entityManager->flush();
return $this->redirectToRoute('todos');
}
return $this->render('todo/todos.html.twig', [
'todos' => $todos,
'form' => $form->createView(),
]);
}
为了在 .twig 中渲染,我只使用了 {{ form(form) }}
尚未对其进行自定义。
编辑:缺少代码
一切看起来都很好。
我在我这边试过了,效果很好(在数据库中,我得到了 null):
表格:
$builder->add(
'dateTime', DateType::class, [
'required' => false,
'widget' => 'single_text',
'empty_data' => ''
]
);
实体
public function __construct() {
// empty
}
/**
* @var DateTime|null
* @ORM\Column(name="date_time", type="datetime", nullable=true)
*/
private ?DateTime $dateTime;
/**
* @return DateTime|null
*/
public function getDateTime(): ?DateTime
{
return $this->dateTime;
}
/**
* @param DateTime|null $dateTime
*
* @return SupportTimeSlot
*/
public function setDateTime(?DateTime $dateTime): SupportTimeSlot
{
$this->dateTime = $dateTime;
return $this;
}
控制器
/**
* @Route("/time-slot-detail/{id}", name="time_slot_detail", methods={"GET", "POST"})
* @param SupportTimeSlot $supportTimeSlot
* @param Request $request
* @param SupportTimeSlotManager $supportTimeSlotManager
*
* @return Response
*/
public function timeSlotDetail(
SupportTimeSlot $supportTimeSlot,
Request $request,
SupportTimeSlotManager $supportTimeSlotManager
): Response
{
$form = $this->createForm(TimeSlotEditType::class, $supportTimeSlot);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$supportTimeSlotManager->save($supportTimeSlot);
return $this->redirectToRoute('boa_support_time_slot_detail', ['id' => $supportTimeSlot->getId()]);
}
return $this->render(
'boa/support/detail.twig', [
'timeSlot' => $supportTimeSlot,
'form' => $form->createView(),
]
);
}
树枝
<div class="row">
<div class="col-12">
{{ form_start(form) }}
{{ form_row(form.dateTime) }}
<button type="submit" class="btn btn-block btn-outline-info">
{% trans %}Save{% endtrans %}
</button>
{{ form_end(form) }}
</div>
</div>
我的项目包含一些datepicker和datetimepicker js,也许尝试实例化js来检查它是否来自这里。
否则尝试在控制器中调试 $request。如果它为您的截止日期属性提供了一些日期,那么您的问题来自 twig/js
如果 $request 为 null 但您的实体填充了截止日期的数据,则您的问题来自您的构造
如果你用空的截止日期保存你的实体,但你在数据库中得到了一个,你的问题来自你的数据库
想要以 symfony 形式添加可选的 DateType class。它有点管用,因为我可以在不设置日期的情况下提交表单,但它会自动设置今天的日期。
TodoType.php
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('name')
->add('Deadline', DateType::class, [
'widget' => 'single_text',
'required' => false,
'empty_data' => ''
])
->add('Submit', SubmitType::class)
;
}
截止实体
/**
* @ORM\Column(type="date", nullable=true)
*/
private $deadline;
...
public function getDeadline(): ?\DateTimeInterface
{
return $this->deadline;
}
public function setDeadline(\DateTimeInterface $deadline = null): self
{
$this->deadline = $deadline;
return $this;
}
TodoController.php
/**
* @Route("/todos", methods={"GET", "POST"}, name="todos")
*
*/
public function todos(EntityManagerInterface $entityManager, Request $request): Response
{
// Rendering todos
$todos = $entityManager->getRepository(Todo::class)
->findBy(
['owner' => $this->getUser()]
);
// Creating new TODO
$todo = new Todo();
$todo
->setOwner($this->getUser())
->setCreationDate(new \DateTime());
$form = $this->createForm(TodoType::class, $todo);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid())
{
$entityManager->persist($todo);
$entityManager->flush();
return $this->redirectToRoute('todos');
}
return $this->render('todo/todos.html.twig', [
'todos' => $todos,
'form' => $form->createView(),
]);
}
为了在 .twig 中渲染,我只使用了 {{ form(form) }}
尚未对其进行自定义。
编辑:缺少代码
一切看起来都很好。
我在我这边试过了,效果很好(在数据库中,我得到了 null): 表格:
$builder->add(
'dateTime', DateType::class, [
'required' => false,
'widget' => 'single_text',
'empty_data' => ''
]
);
实体
public function __construct() {
// empty
}
/**
* @var DateTime|null
* @ORM\Column(name="date_time", type="datetime", nullable=true)
*/
private ?DateTime $dateTime;
/**
* @return DateTime|null
*/
public function getDateTime(): ?DateTime
{
return $this->dateTime;
}
/**
* @param DateTime|null $dateTime
*
* @return SupportTimeSlot
*/
public function setDateTime(?DateTime $dateTime): SupportTimeSlot
{
$this->dateTime = $dateTime;
return $this;
}
控制器
/**
* @Route("/time-slot-detail/{id}", name="time_slot_detail", methods={"GET", "POST"})
* @param SupportTimeSlot $supportTimeSlot
* @param Request $request
* @param SupportTimeSlotManager $supportTimeSlotManager
*
* @return Response
*/
public function timeSlotDetail(
SupportTimeSlot $supportTimeSlot,
Request $request,
SupportTimeSlotManager $supportTimeSlotManager
): Response
{
$form = $this->createForm(TimeSlotEditType::class, $supportTimeSlot);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$supportTimeSlotManager->save($supportTimeSlot);
return $this->redirectToRoute('boa_support_time_slot_detail', ['id' => $supportTimeSlot->getId()]);
}
return $this->render(
'boa/support/detail.twig', [
'timeSlot' => $supportTimeSlot,
'form' => $form->createView(),
]
);
}
树枝
<div class="row">
<div class="col-12">
{{ form_start(form) }}
{{ form_row(form.dateTime) }}
<button type="submit" class="btn btn-block btn-outline-info">
{% trans %}Save{% endtrans %}
</button>
{{ form_end(form) }}
</div>
</div>
我的项目包含一些datepicker和datetimepicker js,也许尝试实例化js来检查它是否来自这里。
否则尝试在控制器中调试 $request。如果它为您的截止日期属性提供了一些日期,那么您的问题来自 twig/js 如果 $request 为 null 但您的实体填充了截止日期的数据,则您的问题来自您的构造 如果你用空的截止日期保存你的实体,但你在数据库中得到了一个,你的问题来自你的数据库