2017-09-05 71 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

相關問題