我通过使用
字段标记为
'mapped' => false
在我的表单生成器上:
protected function configureFormFields(FormMapper $formMapper)
{
$request = $this->getRequest();
$dateParameter = $request->query->get('date');
$date = null;
if ($dateParameter) {
$date = \DateTime::createFromFormat('!Y-m-d', $dateParameter);
}
if (!$date) {
$formMapper
->add('date', DatePickerType::class)
->add('calendarDate', HiddenType::class, array(
'data' => null,
'mapped' => false,
))
;
} else {
$formMapper
->add('date', DatePickerType::class, array(
'data' => $date,
'disabled' => true,
))
->add('calendarDate', HiddenType::class, array(
'data' => $date->format('Y-m-d'),
'mapped' => false,
))
;
}
$formMapper
// other fields
->add('other_field', ModelType::class)
;
}
public function prePersist($myEntity)
{
$this->preUpdate($myEntity);
}
public function preUpdate($myEntity)
{
$dateString = $this->getForm()->get('calendarDate')->getData();
if ($dateString) {
$date = \DateTime::createFromFormat('!Y-m-d', $dateString);
if ($date) {
$myEntity->setDate($date);
}
}
}
date
http://localhost:8000/admin/app/event/24/edit?date=2017-07-01
在HTML中,我有以下内容:
<input type="hidden" id="s59664a9ea44ce_calendarDate" name="s59664a9ea44ce[calendarDate]" class=" form-control" value="2017-07-01" />
P、 当有人给a-1时,最好解释一下为什么。。。