实体的nullable属性为true。表单的required属性为false。生成的HTML没有任何required属性。
当我离开文件输入时,会出现错误:
“找不到文件。”
说实话,我不知道该去哪里找…… ;)
当我离开文件输入时,会出现错误:
“找不到文件。”
说实话,我不知道该去哪里找…… ;)
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('FortiCoreBundle:Article')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Article entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
// $editForm->isVail() false because have error The file could not be found.
if ($editForm->isValid()) {
实体文件:
/**
* @var string
*
* @ORM\Column(name="path", type="string", length=255, nullable=true)
* @Gedmo\UploadableFilePath
*/
private $path;
/**
* @var string
*
* @Assert\File(
* maxSize="3M",
* mimeTypes={"image/png", "image/jpeg", "image/pjpeg"}
* )
*
* @ORM\Column(name="filename", type="string", nullable=true)
* @Gedmo\UploadableFileName
*/
private $filename;
public function uploadPath()
{
return 'images/article/' . date('Y') . '/' . date('m');
}
public function getRelativePath()
{
if (empty($this->filename)) {
return null;
} else {
return $this->uploadPath() . '/' . $this->filename;
}
}
/**
* Set filename
*
* @param string $filename
* @return Article
*/
public function setFilename($filename)
{
if (!is_null($filename)) {
$this->filename = $filename;
}
return $this;
}
/**
* Get filename
*
* @return string
*/
public function getFilename()
{
return $this->filename;
}
以及FormType文件:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
//...
->add('filename', 'file', array(
'label' => 'Zdjęcie',
'required' => false,
'data_class' => null,
'attr' => array(
"class" => 'no-border btn-xs')
))
//...
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Forti\CoreBundle\Entity\Article'
));
}
public function getName()
{
return 'forti_corebundle_articletype';
}