Download default file for Symfony 2

I have a form in my Symfony2 application where I ask the user to upload a file. This field is not required. I am currently processing the file with the following command:

$file = $form['attachement']->getData();

My question is, how can I set the default file if $ file returns NULL, that is, the user does not want to download any file? Thanks.

+4
source share
2 answers

You must first create a valid object from your default file.
To do this, use Symfony \ Component \ HttpFoundation \ File .

Using: $file = new File('path/of/your/default/file');

, .
:

1- FormEvent, , null. (, PRE_SUBMIT POST_SUBMIT)

2- prePersist/preUpdate. ( , "" )

1 . " " ( โ€‹โ€‹ ).

2, . Events Doctrine.
- :

/**
 * @ORM\HasLifecycleCallbacks
 */
class YourEntity {
    /**
     *
     * @ORM\PrePersist
     * @ORM\PreUpdate
     */
    public function manageFile()
    {
        if ($this->attachment === null) {
            $file = new File('path/of/your/default/file');
            $this->setAttachment($file);
        }
    }
}

, .

+1

, Lifecycle Events . prePersist preUpdate $this->file === null .

0

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


All Articles