dongzhu3548 2014-08-10 14:50
浏览 60
已采纳

从控制器获取并传输表单输入值到另一个

I have some troubles with my Symfony Form. In my first controller, I render a form like this:

    $annonce = new Annonce();
    $formBuilder = $this->createFormBuilder($annonce);
    $formBuilder
        ->add('title', 'text')
        ->add('valider', 'submit');
    $form = $formBuilder->getForm();

    $request = $this->get('request');

    if ($request->getMethod() == 'POST') {
        $form->bind($request);

        $response = $this->forward('ProjectMainBundle:Commande:new', array(
            'annonce' => $form["title"]
        ));

        return $response;
    }

And I print the form in the view like this:

<form method="post" {{ form_enctype(form) }}>
    {{ form_row(form.title, {'attr': {'value': entity.mytext }}) }}
    <input type="submit" class="btn btn-primary" />
</form>

That's give:

<form method="post">
    <div>
        <label class="required" for="form_title">Title</label>
        <input id="form_title" type="text" value="11" required="required" name="form[title]">
    </div>
    <input class="btn btn-primary" type="submit">
</form>

So when I submit it, I got the warning

"An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Object of class Symfony\Component\Form\Form could not be converted to string"

I simply want to redirect to the controller "newAction" (that's work), but with the value of the input of my form !

<input id="form_title" type="text" value="11" required="required" name="form[title]">

It seems to I return the form object with

$response = $this->forward('ProjectMainBundle:Commande:new', array(
    'annonce' => $form["title"]
));

But how return just the value of the input of the form ?

Big thanks for help

  • 写回答

1条回答 默认 最新

  • dongqiu3254 2014-08-10 15:07
    关注

    Try calling $annonce->getTitle() instead of using $form["title"]. After calling "$form->bind($request)" your $annonce object should have the title entered in the form.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥18 深度学习tensorflow1,ssdv1,coco数据集训练一个模型
  • ¥100 关于注册表摄像头和麦克风的问题
  • ¥30 代码本地运行正常,但是TOMCAT部署时闪退
  • ¥15 关于#python#的问题
  • ¥15 主机可以ping通路由器但是连不上网怎么办
  • ¥15 数据库一张以时间排好序的表中,找出多次相邻的那些行
  • ¥50 关于DynamoRIO处理多线程程序时候的问题
  • ¥15 kubeadm部署k8s出错
  • ¥15 Abaqus打不开cae文件怎么办?
  • ¥15 小程序准备上线,软件开发公司需要提供哪些资料给甲方