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.

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

报告相同问题?

悬赏问题

  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?
  • ¥20 软件开发方法学习来了