在Symfony 3.3中将数据传递给buildForm()

时间:2017-10-22 04:44:09

标签: php symfony arguments

我正在尝试将当前经过身份验证的用户传递给buildForm,传递几个小时,我搜索了谷歌...没有任何工作 我得到的错误是

  

表单的视图数据应该是AppBundle \ Entity \ AdsList类的实例,但是是(n)数组。您可以通过将“data_class”选项设置为null或通过添加将(n)数组转换为AppBundle \ Entity \ AdsList实例的视图转换器来避免此错误。

on ->getForm();

我必须要做什么...视图变换器...(https://symfony.com/doc/current/form/data_transformers.html

但我只有一个整数...

如果你有一个很好的例子,我也想从内容中生成独特的slu((最终版本没有标题字段):)

提前感谢:)

AgencyController.php

/**
 * @Route("/agency/post", name="agency_post")
 */
public function agencyNewAd(Request $request)
{
   //  $agency = $this->get('security.token_storage')->getToken()->getUser(); ( this didn't worked .. )
    $form = $this->createForm(AgencyNewAdType::class, array(
        'postedBy' => $this->getUser(),
    ));
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {

        $ad = $form->getData();

        // save the task to the database
        $em = $this->getDoctrine()->getManager();
        $em->persist($ad);
        $em->flush();
        // return new Response('Saved new Post with id ' . $ad->getId());
        return $this->redirectToRoute('agency_admin');
    }
    return $this->render('agency/new_ad.html.twig', [
        'adForm' => $form->createView()
    ]);
}

AgencyNewAdType.php

class AgencyNewAdType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
    // https://stackoverflow.com/questions/36905490/how-to-pass-parameter-to-formtype-constructor-from-controller
    $builder
        ->add('title', TextType::class)
        ->add('content', TextareaType::class)
        ->add('category', EntityType::class, array(
            // query choices from Category.Name
            'class' => 'AppBundle:CategoryAd',
            'choice_label' => 'name',
        ))
        ->add('postedAt', DateType::class)
        ->add('postedBy',HiddenType::class, array(
            'data' => $options['postedBy']
        ))
        ->add('save', SubmitType::class, array('label' => 'Create Post'))
        ->getForm();

}
public function configureOptions(OptionsResolver $resolver)
   {
    $resolver->setDefaults(array(
        'postedBy' => null,
        'data_class' => 'AppBundle\Entity\AdsList',
    ));
   }
}

2 个答案:

答案 0 :(得分:1)

我需要将参数传递给表单,以便它看起来像

public function agencyNewAd(Request $request): Response
{
    $pass = new AdsList();
    $pass->setPostedBy($this->getUser());
    $form = $this->createForm(AgencyNewAdType::class, $pass);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        // $form->getData() holds the submitted values iN MeM
        $ad = $form->getData();

        // save the ad to the database
        $em = $this->getDoctrine()->getManager();
        $em->persist($ad);
        $em->flush();
        // return new Response('Saved new Post with id ' . $ad->getId());
        return $this->redirectToRoute('agency_admin');
    }
    return $this->render('agency/new_ad.html.twig', [
        'adForm' => $form->createView()
    ]);
}

并在表格中我需要删除postedBy ...

public function buildForm(FormBuilderInterface $builder, array $options)
{
    // https://stackoverflow.com/questions/36905490/how-to-pass-parameter-to-formtype-constructor-from-controller
    $builder
        ->add('title', TextType::class)
        ->add('content', TextareaType::class)
        ->add('category', EntityType::class, array(
            // query choices from Category.Name
            'class' => 'AppBundle:CategoryAd',
            'choice_label' => 'name',
        ))
        ->add('postedAt', DateType::class)
        ->add('save', SubmitType::class, array('label' => 'Create Post'))
        ->getForm();

}

答案 1 :(得分:0)

在控制器中创建时错过了参数,第二个参数应该是连接到表单的对象,第三个是选项数组,所以它看起来像:

public function agencyNewAd(Request $request)
{
    $ad = new AdsList();
    $form = $this->createForm(AgencyNewAdType::class, $ad, array(
        'postedBy' => $this->getUser(),
    ));
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($ad);
        $em->flush();

        return $this->redirectToRoute('agency_admin');
    }
    return $this->render('agency/new_ad.html.twig', [
        'adForm' => $form->createView()
    ]);
}

另一种方法是将您的选项传递给表单的构造,如下所示:

public function agencyNewAd(Request $request)
{
    $ad = new AdsList();
    $form = $this->createForm(new AgencyNewAdType($this->getUser()), $ad);

然后在您的AgencyNewAdType构造函数中,您将接受您的参数,在这种情况下,当前已登录用户。