如何使用附件[Symfony]编辑表单



我需要带有附件的编辑表单。我在DB中保存了一条附件的途径。在保存路径以保存在表数据库中之后。

我的项目在这里https://github.com/zchipirov/delivery

我的表格:

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

控制器操作:

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>

您必须添加带有文件路径到FileType的" empty_data"以设置默认数据。该解决方案未检查,但我认为它应该起作用

相关内容

  • 没有找到相关文章

最新更新