I'm using Symfony 3.2, and have a little bit of a problem on inserting the JSON data into forms. I have my Note entity, that I need to validate using forms, but I keep getting the error
我正在使用Symfony 3.2,并且在将JSON数据插入表单时遇到一些问题。我有我的Note实体,我需要使用表单进行验证,但我一直收到错误
Cannot use object of type AppBundle\Entity\Note as array
不能使用AppBundle \ Entity \ Note类型的对象作为数组
All I am doing is creating a named builder form, and submitting with a JSON object that I previously decoded, so it's an array now. This is the code:
我正在做的就是创建一个命名的构建器表单,并使用我之前解码的JSON对象提交,所以现在它就是一个数组。这是代码:
$note = new Note();
$form = $this->get('form.factory')
->createNamedBuilder('noteForm', FormType::class, $note)
->add('type', TextType::class, [
'required' => true
])
->add('title', TextType::class, [
'required' => true
])
->add('content', TextType::class, [
'required' => true
])
->getForm()
;
$form->submit($data);
if ($form->isValid()) {
// ...
}
I also tried with the regular FormBuilder, and I made acustom form class, but none of that works, and I keep getting the same error. And yes, I am sending data as a json object:
我也尝试使用常规的FormBuilder,并且我制作了一个自定义表格类,但这些都没有用,我一直得到同样的错误。是的,我将数据作为json对象发送:
{noteForm: {type: "note", title: "something", content: "something"}}
$data declaration is:
$ data声明是:
$data = json_decode($request->getContent(), true);
Can you please help me? Thanks in advance!
你能帮我么?提前致谢!
1 个解决方案
#1
0
Ok, i'm a little bit confused, the problem was that i was sending the $data to a different function and that function is the one in the question above, and i put the @return Note annotation as well. I'm not sure, why it can't work that way, so if anyone can point out for me where i'm wrong. Also one other thing i was mistaken about was sending API call to symfony form. I read on other threads that i had to pass the form name as well as the form fields, but in profiler it said "The form should not contain extra fields", so i just edited the json request, so that it doesn't have form name, only fields, and it's working now. JSON object that i was sending now looks like this: {type: "note", title: "something", content: "something"}
I guess that means that you dont have to send form name anymore in Symfony 3.2? Still i get the CSRF token invalid error, but i don't know how to work that out, except removing CSRF validation from forms. Thanks again, and sorry if this has been a lame question, but i'm a beginner in symfony. :)
好吧,我有点困惑,问题是我将$ data发送到一个不同的函数,该函数是上面问题中的函数,我也放了@return Note注释。我不确定,为什么它不能这样工作,所以如果有人能指出我错在哪里。还有一件事我误以为是向symfony表单发送API调用。我在其他线程上读到我必须传递表单名称以及表单字段,但在分析器中它说“表单不应包含额外字段”,所以我只是编辑了json请求,因此它没有表单名称,只有字段,现在正在运行。我现在发送的JSON对象如下所示:{type:“note”,title:“something”,content:“something”}我想这意味着你不必在Symfony 3.2中发送表单名称了吗?我仍然得到CSRF令牌无效错误,但我不知道如何解决这个问题,除了从表单中删除CSRF验证。再次感谢,对不起,如果这是一个蹩脚的问题,但我是symfony的初学者。 :)
Edit: I figured it out, i don't know why i'm writing this, but if anyone comes across this, it may be helpfull. So, there was a piece of code inside of $form->isValid()
, and that piece of code was the following: $formData = $form->getData(); $note->setType($formData['type']); ...
And the error was "Cannot use object of type AppBundle\Entity\Note as array". The thing i was doing was exactly that i was treating $formData as an array, when in reality, $formData was Note object. So that was my mistake, rookie mistake to be precise. Hope i helped some rookie like myself :)
编辑:我想通了,我不知道为什么我写这个,但如果有人遇到这个,它可能会有所帮助。所以,在$ form-> isValid()中有一段代码,那段代码如下:$ formData = $ form-> getData(); $笔记记录>的setType($ FORMDATA [ '类型']); ...错误是“不能使用AppBundle类型的对象\实体\注意为数组”。我正在做的事情正是我将$ formData视为一个数组,实际上,$ formData是Note对象。所以这就是我的错误,新手的错误是准确的。希望我像我一样帮助一些菜鸟:)
#1
0
Ok, i'm a little bit confused, the problem was that i was sending the $data to a different function and that function is the one in the question above, and i put the @return Note annotation as well. I'm not sure, why it can't work that way, so if anyone can point out for me where i'm wrong. Also one other thing i was mistaken about was sending API call to symfony form. I read on other threads that i had to pass the form name as well as the form fields, but in profiler it said "The form should not contain extra fields", so i just edited the json request, so that it doesn't have form name, only fields, and it's working now. JSON object that i was sending now looks like this: {type: "note", title: "something", content: "something"}
I guess that means that you dont have to send form name anymore in Symfony 3.2? Still i get the CSRF token invalid error, but i don't know how to work that out, except removing CSRF validation from forms. Thanks again, and sorry if this has been a lame question, but i'm a beginner in symfony. :)
好吧,我有点困惑,问题是我将$ data发送到一个不同的函数,该函数是上面问题中的函数,我也放了@return Note注释。我不确定,为什么它不能这样工作,所以如果有人能指出我错在哪里。还有一件事我误以为是向symfony表单发送API调用。我在其他线程上读到我必须传递表单名称以及表单字段,但在分析器中它说“表单不应包含额外字段”,所以我只是编辑了json请求,因此它没有表单名称,只有字段,现在正在运行。我现在发送的JSON对象如下所示:{type:“note”,title:“something”,content:“something”}我想这意味着你不必在Symfony 3.2中发送表单名称了吗?我仍然得到CSRF令牌无效错误,但我不知道如何解决这个问题,除了从表单中删除CSRF验证。再次感谢,对不起,如果这是一个蹩脚的问题,但我是symfony的初学者。 :)
Edit: I figured it out, i don't know why i'm writing this, but if anyone comes across this, it may be helpfull. So, there was a piece of code inside of $form->isValid()
, and that piece of code was the following: $formData = $form->getData(); $note->setType($formData['type']); ...
And the error was "Cannot use object of type AppBundle\Entity\Note as array". The thing i was doing was exactly that i was treating $formData as an array, when in reality, $formData was Note object. So that was my mistake, rookie mistake to be precise. Hope i helped some rookie like myself :)
编辑:我想通了,我不知道为什么我写这个,但如果有人遇到这个,它可能会有所帮助。所以,在$ form-> isValid()中有一段代码,那段代码如下:$ formData = $ form-> getData(); $笔记记录>的setType($ FORMDATA [ '类型']); ...错误是“不能使用AppBundle类型的对象\实体\注意为数组”。我正在做的事情正是我将$ formData视为一个数组,实际上,$ formData是Note对象。所以这就是我的错误,新手的错误是准确的。希望我像我一样帮助一些菜鸟:)