【问题标题】:SYMFONY Hydrate file type for updating instance用于更新实例的 SYMFONY Hydrate 文件类型
【发布时间】:2017-02-17 10:40:45
【问题描述】:

大家好,我正在使用 Symfony 进行一个新项目,当他们有输入文件时,我在编辑视图时遇到了一些困难,当我打开 un 产品的编辑视图时,我的名称输入与产品名称水合,输入价格与产品价格但照片输入文件为空:s

在修改持久化之前我该如何处理这个输入文件

我的表单生成器

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);
    }

控制器

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));

    }

照片类型

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';
    }

树枝视图

<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>

我需要你的帮助,谢谢

【问题讨论】:

    标签: php symfony


    【解决方案1】:

    Symfony 的文件表单类型不会与来自后端的数据一起传播。如果你想显示现有文件,你必须在你的 Twig 文件中手动添加它。

    由于您没有附加实体代码,请注意我的示例应与您的 MyProject\ProductBundle\Entity\Photo 类中的正确方法一起使用。

    在我的示例中,您必须将照片实体作为photo 传递给视图,但前提是之前已经上传过文件。

    <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>
    

    【讨论】:

    • 请改进您的评论,因为它无法理解。
    • 好吧,对不起,我有一个表格用于更新已经保存在数据库中的产品信息,在树枝视图中,我必须在输入名称中填写产品名称和输入文件完成与产品的照片。在你的例子中,我真的不明白这个解决方案是否能解决我的问题,我想我将始终让输入文件为空
    • 是的,文件输入将始终为空。填写此输入的原因是什么?你不应该需要它。
    • 我在更新视图中我需要在控制器中的表单调用之前完成输入,我需要使用数据库中的值完成输入
    • 我了解更新屏幕的基础知识。我只是不明白你为什么坚持传播文件输入?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    • 1970-01-01
    • 2018-09-19
    • 2012-04-29
    相关资源
    最近更新 更多