Symfony2 FileType form entry always returns null

I am working on uploading a .pdf file on my website.

I created an Entity that contains the file name in a string. There is an entity mapping:

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

The form in which I enter the file:

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

View of my form:

{{ 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 } ) }}

And there is a controller:

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()));
    }

The problem is that even if I enter the file when I get the input value with

->salaire->getPdf()

the result is always zero.

At first I thought it was because I set the FileType input form to a string in my entity, but I tried to set it in the UploadedFile variable in my entity, and the result was still null.

Thank you for your help.

+4
source share
2 answers

enctype ( enctype="multipart/form-data").

, - :

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

0

. null. , .

- :

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

, , FileType. :

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

Source: https://habr.com/ru/post/1654816/


All Articles