我在文档中配置了所有内容,如果我上传所有文件,一切都很完美。
但是当我想要更改表格中的其他元素而不更改照片时,我收到了以下错误消息:
您必须传递FileInfoInterface的实例或类的实体的有效数组
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('CmUserBundle:User')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find User entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
$container = $this->container;
if ($editForm->isValid()) {
$uploadableManager = $this->get('stof_doctrine_extensions.uploadable.manager');
$uploadableManager->markEntityToUpload($entity, $entity->getPath());
$em->flush();
return $this->redirect($this->generateUrl('user_edit', array('id' => $id)));
}
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
Form clas:
<?php
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class UserType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email', null, array('attr' => array('class' => 'form-control')))
->add('username', null, array('attr' => array('class' => 'form-control')))
->add('path', 'file', array(
'data_class' => null
))
->add('firstName', null, array('attr' => array('class' => 'form-control')))
->add('lastName', null, array('attr' => array('class' => 'form-control')))
->add('localization', null, array('attr' => array('class' => 'form-control')))
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'CmUserBundle\Entity\User',
));
}
/**
* @return string
*/
public function getName()
{
return 'appbundle_user';
}
}
如何通过空输入字段阻止更新实体?在editForm-&gt; handleRequest($ request);
期间有什么想法吗?
答案 0 :(得分:0)
尝试使用$form->submit($request->request->all(), false)
代替$form->handleRequest($request)
。这样可以防止清除传入POST数据中不存在的实体属性。