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.

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

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法