简体   繁体   English

Symfony-Ajax表单验证-额外数据

[英]Symfony - Ajax form validation - Extra data

I need to send a data from a Symfony form with an Ajax call with an extra Data containing options for the controller. 我需要通过一个带有Ajax调用的Symfony表单发送一个数据,其中包含一个包含控制器选项的额外数据。

I'm working on a custom framework including the Symfony form component. 我正在开发一个包含Symfony表单组件的自定义框架。

So I try in my JS to send values like this : 所以我尝试在我的JS中发送这样的值:

$.ajax
(
    {
        type: "POST",
        url: '/myUrl',
        data: 
        {
            'formData' : $(this).serialize(),
            'extraData' : {'test1' : 'test1', 'test2' : 'test2'}
        },
        success: function(result)
        {
            // do something
        }
    }
);

How can I get my form data in my controller to let the component correctly understand it ? 如何在控制器中获取表单数据以使组件正确理解它? This code won't work : 这段代码不起作用:

$formData = $this->getRequest()->request('formData');
$form->bind($formData);

Thank you. 谢谢。 I hope my question is clear enough 我希望我的问题很清楚

Edit: I finally integrated the data into hidden fields in my form. 编辑:我终于将数据集成到表单中的隐藏字段中。 The form doesn't declare any data_class, as the ORM we use is a really custom one. 该表单没有声明任何data_class,因为我们使用的ORM是一个真正的自定义表单。 Thanks for the answers 感谢您的回答

If your form has a data_class option, you can't bind / handle the request directly with extra_values. 如果您的表单具有data_class选项,则无法直接使用extra_values绑定/处理请求。

Your form expects the variables that you already defined on your formtype. 您的表单需要使用已在表单类型上定义的变量。

Use it like this: 像这样使用它:

if($request->isXmlHttpRequest()) {
    $form->handleRequest($request);

    if($form->isValid()) {
        // To get extra data;
        $extra_data = $request->request->get('extraData');
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM