使用AJAX进行表单调用

时间:2014-10-30 15:45:41

标签: ajax forms symfony

当我尝试使用ajax发送表单时遇到问题。

我的树枝:

<div class="admin_creation"></div>


<script type="text/javascript">

    $('.admin_creation').ready(
    function () {
        var id = {{ elem.id }}
        $.ajax({
            data:'id='+id,
            url: Routing.generate('creation_update'),
            success: function(response){
                console.log('ok');
                $('.admin_creation').prepend(response);
            },
            error: function(){
                console.log('fail');
            }
        });
    })

</script>

我的路线:

creation_update:
    path: /detail/update
    defaults: { _controller: NemiTwigBundle:Default:update }
    options:
        expose: true

我的控制员:

public function updateAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $id = $request->query->get('id');
    $creation = $em->getRepository('NemiTwigBundle:Creation')->findOneById($id);

    //at this point, if i print_r($creation), there is nothing. The $id is correct.
    //when i print($creation), the __tostring method is working and display the right
    //name of the creation.


    $form = $this->createFormBuilder()->add('price', 'integer')
                                      ->add('submit', 'submit')
                                      ->getForm();

    $form->handleRequest($request);

    if ($form->isValid())
    {
        $creation->setPrice($form['price']->getData());
        $em->persist($creation);
        $em->flush();

        return new Response();

        return $this->redirect($this->generateUrl('creation_detail', array('id' => $id)));
    }

    return $this->render('NemiTwigBundle::creation_update_form.html.twig', array('form' => $form->createView()));
}

形式:

<form action="{{ path('creation_update') }}" {{ form_enctype(form) }} method="POST">
    {{ form(form) }}
</form>

我的实际问题是我在控制器中的id是正确的id,有创建实体有这个id但是当我尝试用doctrine加载这个创建时,它不起作用(我认为它不适用于私有属性和方法导致当我尝试print($creation)时,__toString方法工作正常并给出正确的结果。)

编辑:第一个答案,

我按照你的建议尝试使用此控制器:

public function updateAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();
    $id = $request->query->get('id');
    $creation = $em->getRepository('NemiTwigBundle:Creation')->findOneById($id);

    $form = $this->createFormBuilder($creation)->add('price', 'integer')
                                      ->add('submit', 'submit')
                                      ->getForm();

    $form->handleRequest($request);

    if ($form->isValid())
    {
        $em->persist($creation);
        $em->flush();

        return $this->redirect($this->generateUrl('creation_detail', array('id' => $id)));
    }

    return $this->render('NemiTwigBundle::creation_update_form.html.twig', array('form' => $form->createView()));

但我收到了这个错误:

类型为“string,Symfony \ Component \ Form \ ResolvedFormTypeInterface或Symfony \ Component \ Form \ FormTypeInterface”,“Nemi \ TwigBundle \ Entity \ Creation”的预期参数

1 个答案:

答案 0 :(得分:1)

$form = $this->createFormBuilder($creation)->add('price', 'integer')
                                      ->add('submit', 'submit')
                                      ->getForm();

问题是您目前在没有实体的情况下创建表单。

你基本上做的是

  1. 加载实体
  2. 创建表单
  3. 将请求中的变量放入
  4. 格式
  5. 保存实体。
  6. 您没有将实体添加到表单中。 如果使用加载的实体创建表单,则会将更改的值添加到实体。