Let’s say I have a nullable datetime field in my entity:
class Foo
{
/**
* @ORM\Column(name="due", type="datetime", nullable=true)
*/
private $due;
public function getDue()
{
return $this->due;
}
public function setDue($due)
{
$this->due = $due;
return $this;
}
}
And now in the FooType.php
I define a form field for this:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// ...
->add('due', 'datetime')
// ...
;
}
However, even though the field is nullable, the form field rendered will allow the user only to enter a date/time, but not to say that it should be null
.
I would like to have either a radio button UI like this:
( ) No due date
( ) Due date: [ control ]
or perhaps a checkbox:
[ ] Due date: [ control ]
Ideally, if “no due date” is selected/the checkbox is unchecked, the control should be disabled.
Is it possible to do this elegantly in Symfony?
Afaik theres no symfony provided solution
i would
$builder
// ...
->add('due', 'hidden')
and in view you implement the logic with html and js
so two checkboxes on click on due-date you show a datetime input then
you just need to set the value (null || thedatetimeinputvalue) to the hidden field before submit