Question

I have an entity that is created through a form with file fields. Now when I want to update this entity through the same form, I need to reupload the files. How could I do to pre-fill the file fields or not modifiy these attributes when linving the fields blank?

EDIT : FormType :

class ThemeType extends AbstractType {

    private $edit;
    private $parentFunctionalId;

    public function __construct($parentFunctionalId = null, $edit = false) {
        $this->edit = $edit;
        $this->parentFunctionalId = $parentFunctionalId;
    }

    public function buildForm(FormBuilderInterface $builder, array $options) {

        $builder->add('translations', 'collection', array('type' => new ThemeHasTranslationsType(),'allow_add' => true))
            ->add('image1', 'file', array(
                'data_class' => null, 'required' => false
            ))
            ->add('image2', 'file', array(
                'data_class' => null, 'required' => false
            ))
            ->add('style', new StyleType())
            ->add('isVerb', 'checkbox', array('label' => 'Verbe', 'required' => false))
            ->add('isActive', 'checkbox', array('label' => 'Activé', 'required' => false));

            $action = $this->parentFunctionalId != null ? $this->parentFunctionalId : '/app_dev.php/themes';
            $submitLabel = $this->edit ? 'edit' : 'create';
            $builder->setAction($action)
                    ->add($submitLabel, 'submit');

    }

    public function setDefaultOptions(OptionsResolverInterface $resolver) {
        $resolver->setDefaults(array(
            'data_class' => 'MyApp\BusinessBundle\Entity\Theme'
        ));
    }

    public function getName() {
        return 'translations';
    }

}
Was it helpful?

Solution

You should use a second attribute $image1Filename containing the filename of the file uploaded.

This way, you can check if the form contains a file when posted, and "manually" handle the file upload (via lifecycle callbacks events eventually).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top