dtuct88226 2017-02-17 10:40
浏览 31

SYMFONY用于更新实例的Hydrate文件类型

Hi everybody I'm working onn a new project with Symfony and i have some difficulties with the edit views when they have an input file, when i open edit view for un product i have the name input hydrated with the name of product, the input price with the price of product but the photo input file empty :s

How can i do to hydarat this input file before the persist of the modification

My FormBuilder

class ProductEditType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
        ->add('name',TextType::class)
        ->add('price',TextType::class)
        ->add('Photo',PhotoType::class)
        ->add('enregistrer',SubmitType::class);
    }

the controller

public function editProductAction($id, Request $request){
        $product = new Product();
        $repository=$this->getDoctrine()->getManager()->getRepository('MyProjectProductBundle:Product');
        $product= $repository->find($id);
        $form=$this->get('form.factory')->create(ProductEditType::class,$product);
        if($request->isMethod('post') && $form->handleRequest($request)->isValid()){

            $em=$this->getDoctrine()->getManager();
            $em->persist($product);
            $em->flush();
            return $this->redirectToRoute('my_project_backoffice_product', array('id'=>$product->getId()));
        }

        return $this->render('MyProjectBackofficeBundle:Backoffice:editproduct.html.twig',array('form'=>$form->createView(),'product'=>$product));

    }

PhotoType

class PhotoType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('file', FileType::class)     ;
    }
    
    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'MyProject\ProductBundle\Entity\Photo'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'myproject_productbundle_photo';
    }

twig view

<div class="well">

   {{ form_start(form) }}
 

<div class="container4_in">
    <div class="gri4">
        <div class="grid2">
        <div class="form_elem_wrraper">

    <div class="row_form">
    {{ form_label(form.name, "Name ") }}
    {# Génération de l'input. #}
    {{ form_widget(form.name,{ 'attr': {'class' :'input', 'placeholder': "Entrez le nom de l'auteur"}}) }}
    </div>

    
</div></div>



<div class="grid2">
        <div class="form_elem_wrraper">

   <div class="row_form">
    {{ form_label(form.price, "Price ") }}
    {# Génération de l'input. #}
    {{ form_widget(form.price,{ 'attr': {'class' :'input', 'placeholder': "Entrez le nom de l'auteur"}}) }}
    </div>

    <div class="row_form">
    {{ form_label(form.Photo, "Photo") }}
    {# Génération de l'input. #}
    {{ form_widget(form.Photo) }}
    </div>

    

</div>
</div>


</div>
</div>

  {# Pour le bouton, pas de label ni d'erreur, on affiche juste le widget #}
  {{ form_widget(form.enregistrer,{'attr': {'class': 'btn_bleu btn_submit_form'}}) }}

  {# Fermeture de la balise <form> du formulaire HTML #}
  {{ form_end(form) }}

                                
                            </form>

I need your help, Thank you

</div>
  • 写回答

1条回答 默认 最新

  • dqrqp8492 2017-02-17 12:48
    关注

    Symfony's file form types are not propagated with data from backend. If you would like to show existing file, you have to add it manually in your Twig file.

    As you didn't attached your entity code, please note that my example should be used with proper methods from your MyProject\ProductBundle\Entity\Photo class.

    In my example you have to pass to the view Photo entity as photo, but only if there was already uploaded file before.

    <div class="row_form">
        {% if photo is defined %}
            Current photo: <img src="{{ photo.getWebPath() }}" />
        {% endif %}
    
        {{ form_label(form.Photo, "Photo") }}
        {# Génération de l'input. #}
        {{ form_widget(form.Photo) }}
    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?