2017-09-05 70 views
0

我已经创建了2个字段(名称和文件)的窗体。我遵循本指南https://symfony.com/doc/current/controller/upload_file.htmlSymfony窗​​体editAction与fileupload不工作,因为没有文件

我已经创建了我的CRUD。我的addAction没问题。但我的编辑行为并不好。当我有效的表单正确改变名称而不是文件(输入文件上没有文件)时,表单发送错误“没有文件”。如何在没有新文件的情况下使editAction工作,如果文件没有变化?

添加我的项目文件

ProductController.php EditAction()

/** 
* Displays a form to edit an existing product entity. 
* 
* @Route("/{id}/edit", name="product_edit") 
* @Method({"GET", "POST"}) 
*/ 
public function editAction(Request $request, Product $product) 
{ 
    $deleteForm = $this->createDeleteForm($product); 
    $editForm = $this->createForm('AppBundle\Form\ProductType', $product); 
    $editForm->handleRequest($request); 

    if ($editForm->isSubmitted() && $editForm->isValid()) { 
     $this->getDoctrine()->getManager()->flush(); 

     return $this->redirectToRoute('product_edit', array('id' => $product->getId())); 
    } 

    return $this->render('product/edit.html.twig', array(
     'product'  => $product, 
     'edit_form' => $editForm->createView(), 
     'delete_form' => $deleteForm->createView(), 
    )); 
} 

ProductControlle newAction()

/** 
* Creates a new product entity. 
* 
* @Route("/new", name="product_new") 
* @Method({"GET", "POST"}) 
*/ 
public function newAction(Request $request) 
{ 
    $product = new Product(); 
    $form = $this->createForm(
     'AppBundle\Form\ProductType', 
     $product, 
     array(
      'validation_groups' => array('add') 
     ) 
    ); 
    $form->handleRequest($request); 

    if ($form->isSubmitted() && $form->isValid()) { 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($product); 
     $em->flush(); 

     return $this->redirectToRoute('product_show', array('id' => $product->getId())); 
    } 

    return $this->render('product/new.html.twig', array(
     'product' => $product, 
     'form' => $form->createView(), 
    )); 
} 

我的产品实体

/** 
* @var int 
* 
* @ORM\Column(name="id", type="integer") 
* @ORM\Id 
* @ORM\GeneratedValue(strategy="AUTO") 
*/ 
private $id; 


/** 
* @var string 
* 
* @ORM\Column(name="name", type="string", length=255) 
* @Assert\Type(type="string") 
* @Assert\NotBlank() 
*/ 
private $name; 

/** 
* @ORM\Column(type="string") 
* 
* @Assert\NotBlank(message="Please, upload the product brochure as a PDF file.", groups={"add"}) 
* @Assert\File(mimeTypes={ "application/pdf" }, groups={"add"}) 
*/ 
private $brochure; 

/** 
* Get id 
* 
* @return int 
*/ 
public function getId() 
{ 
    return $this->id; 
} 

/** 
* Set name 
* 
* @param string $name 
* 
* @return Product 
*/ 
public function setName($name) 
{ 
    $this->name = $name; 

    return $this; 
} 

/** 
* Get name 
* 
* @return string 
*/ 
public function getName() 
{ 
    return $this->name; 
} 

/** 
* Set brochure 
* 
* @param string $brochure 
* 
* @return Product 
*/ 
public function setBrochure($brochure) 
{ 
    $this->brochure = $brochure; 

    return $this; 
} 

/** 
* Get brochure 
* 
* @return string 
*/ 
public function getBrochure() 
{ 
    return $this->brochure; 
} 

我FileUp装填器(服务)

private $targetDir; 

public function __construct($targetDir) 
{ 
    $this->targetDir = $targetDir; 
} 

public function upload(UploadedFile $file) 
{ 
    $fileName = md5(uniqid()) . '.' . $file->guessExtension(); 

    $file->move($this->getTargetDir(), $fileName); 

    return $fileName; 
} 

public function getTargetDir() 
{ 
    return $this->targetDir; 
} 

而且我BrochureUploadListener.php

private $uploader; 
private $fileName; 

public function __construct(FileUploader $uploader) 
{ 
    $this->uploader = $uploader; 
} 

public function prePersist(LifecycleEventArgs $args) 
{ 
    $entity = $args->getEntity(); 

    $this->uploadFile($entity); 
} 

public function preUpdate(PreUpdateEventArgs $args) 
{ 
    $entity = $args->getEntity(); 

    $this->uploadFile($entity); 
} 

public function postLoad(LifecycleEventArgs $args) 
{ 
    $entity = $args->getEntity(); 

    if (!$entity instanceof Product) { 
     return; 
    } 

    if ($fileName = $entity->getBrochure()) { 
     $entity->setBrochure(new File($this->uploader->getTargetDir().'/'.$fileName)); 
    } 
} 

private function uploadFile($entity) 
{ 
    // upload only works for Product entities 
    if (!$entity instanceof Product) { 
     return; 
    } 

    $file = $entity->getBrochure(); 

    // only upload new files 
    if ($file instanceof UploadedFile) { 
     $fileName = $this->uploader->upload($file); 
     $entity->setBrochure($fileName); 
    } 
} 

而且我services.yml

AppBundle\Service\FileUploader: 
    arguments: 
     $targetDir: '%brochures_directory%' 

AppBundle\EventListener\BrochureUploadListener: 
    tags: 
     - { name: doctrine.event_listener, event: prePersist } 
     - { name: doctrine.event_listener, event: preUpdate } 
     - { name: doctrine.event_listener, event: postLoad } 

回答

0

我已经使用vichuploader包,它更简单,可以。

0

的问题是,当你使用

$form->isValid() 

它实际验证

@Assert\NotBlank(message="Please, upload the product brochure as a PDF file.") 
@Assert\File(mimeTypes={ "application/pdf" }) 

但您提供字符串而不是UploadedFile的实例。你可以做的是创建验证组,以确保这一断言只会工作,当你创建新的实体:

@Assert\NotBlank(message="Please, upload the product brochure as a PDF file.", groups={"add"}) 
@Assert\File(mimeTypes={ "application/pdf" }, groups={"add"}) 

,然后添加到您的addAction下面的行内窗体选项:

'validation_groups' => array('add') 

所以,addAction里面的表单实例应该看起来像这样:

$form = $this->createForm(YourFormType::class, null, array(
    'validation_groups' => array('add') 
)); 
+0

嗨,谢谢。验证组是可以的。但是,当我只编辑名称时,我丢失了实体上的文件。 :( – eldiablo62

+0

你能否给我们提供一些描述你的updateAction的代码,以便更好地理解是什么导致了这个问题? –

+0

我已经用我的不同文件更新了我的文章,我已经删除了文件的标题和评论,因为渲染效果不好。 – eldiablo62

相关问题