Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Symfony 3.4使用RESTful API插入数据,无需绑定_Php_Ajax_Rest_Symfony_Symfony 3.4 - Fatal编程技术网

Php Symfony 3.4使用RESTful API插入数据,无需绑定

Php Symfony 3.4使用RESTful API插入数据,无需绑定,php,ajax,rest,symfony,symfony-3.4,Php,Ajax,Rest,Symfony,Symfony 3.4,我在将数据插入数据库时遇到问题 我可以使用此代码插入: $quote->setSource($data['quote_form[source]']); $quote->setQuote($data['quote_form[quote]']); 我想要的是在不添加特定字段的情况下插入数据 问题: 我的第一个代码中的问题是,如果我要添加一个新字段,我需要一次又一次地添加一个新集合 当我添加if($form->isSubmitted()&&&&$form->isValid())时,它不会

我在将数据插入数据库时遇到问题

我可以使用此代码插入:

$quote->setSource($data['quote_form[source]']);
$quote->setQuote($data['quote_form[quote]']);
我想要的是在不添加特定字段的情况下插入数据

问题:

  • 我的第一个代码中的问题是,如果我要添加一个新字段,我需要一次又一次地添加一个新集合
  • 当我添加
    if($form->isSubmitted()&&&&$form->isValid())
    时,它不会进入if条件
  • 这是我在控制器中的函数代码:

        public function createAction(Request $request)
        {
            if ($request->getMethod() === 'POST') {
                $data = json_decode($request->getContent(), true);
                $quote = new Quote();
                $form = $this->createForm(QuoteForm::class, $quote)->submit($data);
    
                if ($form->isSubmitted() && $form->isValid()) {
                    $em = $this->getDoctrine()->getManager();
    
                    // Add our quote to Doctrine so that it can be saved
                    $em->persist($quote);
    
                    // Save our quote
                    $em->flush();
                } else {
                    echo 'not valid';die();
                }
    
                return new Response('It\'s probably been saved', 201);
            } else {
                throw $this->createNotFoundException();
            }
        }
    
    对于Html Ajax:

    function serializeFormJSON(formArray) {
        var returnArray = {};
        for (var i = 0; i < formArray.length; i++) {
            returnArray[formArray[i]['name']] = formArray[i]['value'];
        }
    
        return returnArray;
    }
    
    var form = $('form[name=quote_form]');
        form.submit(function (e) {
            console.log(JSON.stringify(serializeFormJSON($(this).serializeArray())));
            e.preventDefault();
            $.ajax({
                type: 'POST',
                url: $(this).attr('action'),
                contentType: 'application/json; charset=utf-8',
                data: JSON.stringify(serializeFormJSON($(this).serializeArray())),
                dataType: 'json',
                success: function(response) {
                    console.log(response);
                },
            });
        });
    
    函数序列化FormJSON(formArray){
    var returnArray={};
    对于(变量i=0;i
    我也有这个想法

    ->


    ->您的表单似乎无效。表单对象中没有方法

    $form->getErrors();
    
    它返回验证错误的集合。symfony profiler中还有一节与表单验证错误相关:

    谢谢,我在验证中发现了问题。我只是禁用了csrf_令牌