3.3
我试图当前身份验证的用户传递到buildForm的通几个小时,我搜索谷歌在Symfony的数据传递到buildForm()...没有什么工作 我得到的错误是3.3
The form's view data is expected to be an instance of class AppBundle\Entity\AdsList, but is a(n) array. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) array to an instance of AppBundle\Entity\AdsList.
上->getForm();
我必须IDEEA该怎么办...视图变压器...(https://symfony.com/doc/current/form/data_transformers.html)
,但我只有一个整数...
我也想生成内容独特蛞蝓(最终版本不会有一个标题字段),如果你有一个很好的例子:)
在此先感谢:)
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://*.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',
));
}
}
ÿ OU从控制器创建你的时候,第二个参数应该是你的目标连接的形式和第三的是选项数组错过了一个参数,所以它看起来像:
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的构造函数,你会接受你的参数,在这种情况下,当前登录的用户。
我需要的参数传递到窗体所以它看起来像
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://*.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();
}
对于第一种选择我有一个10页错误 - > '渲染模板期间抛出异常(“Catchable致命错误:类AppBundle \ Entity \ Agency的对象无法转换为字符串”)。' –
和第二个 - > '键入呃ror:传递给Symfony \ Bundle \ FrameworkBundle \ Controller \ Controller :: createForm()的参数3必须是在/home/dev/www/d/src/AppBundle/Controller/AgencyController.php中调用的类型数组,对象在线46' line 46 '$ form = $ this-> createForm(AgencyNewAdType :: class,($ this-> getUser()),$ ad);' –
@AlexanderBr。第一个错误看起来没有连接到表单,因为我没有看到你提到代理实体的任何地方,但我想你需要一个toString方法在该实体 – kunicmarko20