我有一个带有字段“图像”的实体“Cours” 这是CoursType.php的构建器
$builder
->add('titre')
->add('image', FileType::class, array('data_class' => null, 'label'=>false,'required'=>false))
->add('videoIntro')
);
问题在于“”编辑表单“”,当我提交此表单而不上传新图像时,空值将传递给“图像”。
这是editAction的控制器代码
public function editAction(Request $request, Cours $cour)
{
$em = $this->getDoctrine()->getManager();
$deleteForm = $this->createDeleteForm($cour);
$editForm = $this->createForm('LearnBundle\Form\CoursType', $cour);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid())
{
$image=$cour->getImage();
$imageName = md5(uniqid()).'.'.$image->guessExtension();
$image->move($this->getParameter('images_directory'),$imageName);
$cour->setImage($imageName);
$em->flush();
return $this->redirectToRoute('cours_edit', array('id' => $cour->getId()));
}
return $this->render('cours/edit.html.twig', array(
'cour' => $cour,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}