2016-09-16 3 views
4

내 웹 사이트에서 .pdf 파일을 업로드하는 방법을 찾고 있습니다.Symfony2 FileType 입력 형식은 항상 null을 반환합니다.

문자열의 파일 이름을 포함하는 엔티티를 만들었습니다. 엔티티 매핑 있습니다 :

<field name="pdf" type="string" column="pdf" length="200" nullable="true"/> 

양식 어디 입력 파일 :

$builder 
     ->add('pdf', FileType::class, array('label' => 'Fiche de paie')) 
     /* other input */ 
    ; 

내 양식의보기 :

{{ form_start(form) }} 
    {{ form_row(form._token) }} 
    <div class="modal-body row"> 
     <div class="col-md-6 col-sm-8"> 
      /* other input */ 
      {{ form_row(form.pdf) }}} 
     </div> 
     <div class="col-md-6 col-sm-8"> 
      /* other input */ 
     </div> 
    </div> 

    <div class="modal-footer"> 
     <button type="button" class="btn btn-info" id="save">{% trans %}Save{% endtrans %}</button> 
    </div> 
    {{ form_end(form, { 'render_rest': false }) }} 

그리고 컨트롤러가 :

if ($form->isSubmitted() && $form->isValid()) { 
     $pdf = $salaire->getPdf(); 

     if ($pdf == null) { /* Always true */ 
      $salaire->setPdf('test'); 
     } 

     /* operations to extract the file name and set it to the pdf variable in salaire */ 

     $em->persist($salaire); 
     $em->flush(); 

     return $this->redirectToRoute('salaires_index', array("id" => $remuneration->getId())); 
    } 

T 그는 파일을 입력하더라도 입력 값을 얻을 때

->salaire->getPdf() 

결과는 항상 null입니다.

처음 엔 내 엔티티에있는 문자열에 양식 입력 FileType을 설정했기 때문에 생각했지만 필자는 엔티티에서 UploadedFile 변수에 값을 설정하려고 시도했지만 그 결과는 여전히 null입니다.

도움 주셔서 감사합니다.

답변

0

form 태그의 올바른 enctype (양식 태그에 enctype="multipart/form-data")을 설정해야합니다.

을 참조하면 documentation 당신이 좋아하는 일을 할 수 있습니다

{{ form_start(form, {'multipart': true}) }} 

희망 나는 비슷한 문제가 있었이 도움

+0

도움을 주셔서 감사합니다.하지만 도움이되지 않았습니다. – Julien

0

. 업로드 된 파일은 항상 null 값을가집니다. 컨트롤러의 양식 처리가 올바르지 않습니다. 이 일하고있다, 그래서 내가 파일 유형 요소를 추가 할 때까지

if ($request->isMethod('POST')) { 
    $form->submit($request->request->get($form->getName())); 
    if ($form->isSubmitted() && $form->isValid()) { 
    // handle form data 
    } 
} 

다른 모든 필드는 자신의 가치를 제공 :

원래 내가 이런 일을했다. 문제가 해결되었습니다.

$form->handleRequest($request); // This bit was important 
if ($form->isSubmitted() && $form->isValid()) { 
// handle form data 
}