2017-01-30 42 views
1

我在編輯具有文件的實體時遇到問題。 我遵循Symfony文檔上傳文件,它工作正常。 創建時,我的文件存儲在我的上傳文件夾中,加密名稱存儲在數據庫中。Symfony 3 - 文件編輯窗體不顯示照片名稱

對於版我也跟着文檔,以免有任何錯誤,我引用:

當創建一個表單編輯一個已經持久化項目,該文件表單類型仍然期望一個文件實例。由於持久化實體現在僅包含相對文件路徑,因此首先必須將配置的上載路徑與存儲的文件名連接起來,然後創建一個新的文件類

然後運行。

但形式的顯示屏上,該文件字段說 「沒有文件選擇」

這裏是我的控制器:

public function editAction(Request $request, Produits $entity) 
    { 
     $entity->setPhoto(
      new File($this->getParameter('images_directory').'/'.$entity->getPhoto()) 
     ); 
     $form = $this->createForm('ProjectBundle\Form\Produit\ProduitType', $entity); 

     $form->handleRequest($request); 
     if ($form->isSubmitted()) { 
      if ($form->isValid()) { 
       $em = $this->getDoctrine()->getManager(); 
       $em->persist($entity); 
       $em->flush(); 

       $this->addFlash('success', 'Le produit "'.$entity.'" a été modifié.'); 

       return $this->redirectToRoute('produit_show', array('id' => $entity->getId())); 
      } 
     } elseif ($form->isSubmitted()) { 
      $this->addFlash('error', 'Le produit "'.$entity.'" n\'a pas pu être modifié.'); 
     } 

     return $this->render('ProjectBundle:admin/Produit:edit.html.twig', array(
      'entity' => $entity, 
      'form' => $form->createView(), 
     )); 
    } 

在我看來:

<div class="input-field{% if form.photo.vars.errors|length %} has-error{% endif %}"> 
    {{ form_widget(form.photo,{'attr': {'class': 'form-control'}}) }} 
    {{ form_errors(form.photo) }} 
</div> 

如果我這樣做一個{{ dump(form.photo) }}我得到關於屬於我的實體的照片的所有信息。

您是否有想法在圖片中獲取圖片的名稱?

謝謝!

+0

嗨,你有沒有找到解決辦法?我堆積在同一點:-( – mario

+0

其實,我認爲你不能「獲得該字段中的圖像的名稱」,由[這些原因](http://stackoverflow.com/questions/1696877/how-您可能希望顯示圖片,比如說,在「No file selected」字符串的下方顯示圖片,特別是如果您有一個圖片文件,形式來一次編輯多個對象,這將是很好的顯示不同的圖片。不幸的是,我不知道如何做到這一點。 – mario

+0

確實,令人費解的問題是爲什麼似乎不可能顯示像{{形式.photo.name}},雖然很清楚如何顯示{{dump(form.photo)}},後者也顯示了photo.name。 – mario

回答

0

這是最邏輯的行爲......

文件中顯示的文件類型輸入是本地文件(從您的計算機),您將它們發送到服務器之前。

如果您想「顯示」當前文件,請添加一個自定義字段,該字段在存在文件時顯示文件名。

+0

「添加自定義字段」,怎麼樣? – mario

+0

如何...在表單中添加如下內容:'{%if form.photo | length> 0%} {%endif%}' – Preciel

+0

謝謝,但不是,我不明白。請參閱[我的問題](http://stackoverflow.com/questions/42149429/symfony-3-1-and-forms-how-to-display-data)。 – mario

0

此行不似乎是正確的

$entity->setPhoto(
     new File($this->getParameter('images_directory').'/'.$entity->getPhoto()) 
    ); 

如果要調用$實體 - > getPhoto()可暗示一個文件已經存在,但是你是一個新的文件設置爲實體。我的想法是該文件沒有找到,因此沒有顯示名稱。

如果添加了FormType和Model,它可能有助於確定問題。

+0

不,這正是您建議由[docu](http://symfony.com/doc/current/controller/upload_file.html)執行的操作。您正在設置一個新文件,其中包含有關之前上傳的文件的信息。這很好,因爲你想渲染一個** edit **表單。但是,對我而言,如何在表單中插入信息的方式並不清楚。 – mario

0

我已經做了一個workaorund來解決這個問題,我無法通過編輯窗體中的文件,所以我顯示當前的圖像和萬一用戶不上傳一個新的保存舊的。

我有一個陸軍單位,在我的樹枝我打電話與

<img style="max-width:128px" src={{ asset('uploads/army/' ~ army.image.filename) }}> 

的圖像,並在控制器我的編輯功能。

public function editAction(Request $request, Army $army) 
{ 
    $army->setImage(new File($this->getParameter('army_directory') . '/' . $army->getImage())); 
    $imgOri = basename($army->getImage()); 
    $deleteForm = $this->createDeleteForm($army); 
    $editForm = $this->createForm('ArmyDataBundle\Form\ArmyType', $army); 
    $editForm->handleRequest($request); 

    if ($editForm->isSubmitted() && $editForm->isValid()) { 

     if ($army->getImage()) { 
      // $file stores the uploaded PDF file 
      /** @var Symfony\Component\HttpFoundation\File\UploadedFile $file */ 
      $file = $army->getImage(); 

      // Generate a unique name for the file before saving it 
      $fileName = md5(uniqid()) . '.' . $file->guessExtension(); 

      // Move the file to the directory where army are stored 
      $imgRoute = $this->container->getParameter('army_directory'); 
      $file->move(
       $imgRoute, 
       $fileName 
      ); 
      $army->setImage($fileName); 
     } else { 

      $army->setImage($imgOri); 
     } 
     $this->getDoctrine()->getManager()->persist($army); 
     $this->getDoctrine()->getManager()->flush($army); 

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

    return $this->render('@ArmyData/Army/edit_army.html.twig', array(
     'army' => $army, 
     'edit_form' => $editForm->createView(), 
     'delete_form' => $deleteForm->createView(), 
    )); 
}