2

I need edit form with attachment. I saved a path to attachment in DB. After save path to file save in table database.

My project is here https://github.com/zchipirov/delivery

My Form:

 ->add('photo', FileType::class)

Controller action:

public function newAction(Request $request) {
        $em = $this->getDoctrine()->getManager();
        $repo = $em->getRepository('DeliveryAdminBundle:MyEntity');
        $_arr = $request->request->get('my_entity');

        $entity = new MyEntity();
        $form = $this->createForm(MyEntityType::class, $entity, [
            'entity_manager' => $this->get('doctrine.orm.entity_manager')
        ]);

        $kitchen_array = $_arr['kitchen'];
        $specialization_array = $_arr['specialization'];

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $dir = './entity/'.Additional::get_in_translate_to_en($entity->getTitle());
            if (!file_exists($dir)) {
                mkdir ($dir);
            }
            // $dir .= '/'.Additional::get_in_translate_to_en($entity->getTitle()).'.jpg';
            $file_name = Additional::get_in_translate_to_en($entity->getTitle()).'.jpg';
            $form['photo']->getData()->move($dir.'/', $file_name);

            $identity = $repo->save($entity, $dir.'/'.$file_name);
            $repo_spec = $em->getRepository('DeliveryAdminBundle:MyEntitySpecialization');
            $repo_spec->add($identity, $specialization_array);

            $repo_kitchen = $em->getRepository('DeliveryAdminBundle:MyEntityKitchen');
            $repo_kitchen->add($identity, $kitchen_array);

            return $this->redirectToRoute('show_myentity');
        }

        return $this->render( 'DeliveryAdminBundle:Entity:new.html.twig', 
                array('form'=>$form->createView()) );
    }

html:

<div class="form-group">
                        <label>
                            {{ form_label(form.photo, 'Логотип заведения') }}
                        </label>
                            {{ form_errors(form.photo) }}
                            <span class="btn btn-success fileinput-button"> <i class="glyphicon glyphicon-plus"></i>
                                <span>Добавить файл...</span>
                                {{ form_widget(form.photo, { 'attr': {'class': 'form-control'} }) }}
                            </span>                         
                            <br/>
                            <br/>
                            {{ form_widget(form.save, { 'attr': {'class': 'btn btn-primary btn-sm'} }) }}
                            <button class="btn btn-wide btn-sm" onclick="location='{{ path('show_kitchen') }}';">Отмена</button>
                    </div>
Zaur
  • 47
  • 6
  • @Rooneyl is there a difference in this respect between Symfony 2 and symfony3? If so, and the duplicated original still works then it's title should be reworded to remove the version2 specific wording. – Martin Feb 15 '17 at 20:25

1 Answers1

1

You must add "empty_data" with path to file to FileType to set default data. This solution doesn't checked but I think it should work

  • Error: The form's view data is expected to be an instance of class Symfony\Component\HttpFoundation\File\File, but is a(n) string. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) string to an instance of Symfony\Component\HttpFoundation\File\File. – Zaur Feb 15 '17 at 12:39