Symfony:如何将 Doctrine Collection 传递给 Form 作为默认值?
我的操作:
public function executeEdit(sfWebRequest $request)
{
// Get the object
$article = $this->getRoute()->getObject();
// Create the form
$this->form = new ArticleEditForm( $article );
}
您可以看到 $article
是作为默认值传递到表单的 Doctrine 集合。 $article
对象包含“title”、“text”、“author”等字段。
但这会导致表单创建时出现错误:
500 | Internal Server Error | Doctrine_Record_UnknownPropertyException
Unknown record property / related component "_csrf_token" on "article"
所以基本上,表单正在尝试使用 Doctrine用于填写表单元素默认值的集合。但该对象中显然没有 csrf_token
...但它仍在尝试找到一个用作默认值...
如果您有一个总是有额外值的表单,会发生什么空字段为空,但其他字段具有传递的默认值。如果这些空字段在 Doctrine Collection 中没有设置值,那么您会收到错误...
现在,显然我可以提前创建一个简单的数组,在其中指定默认值并传递该数组:
$defaults = array( 'title' => $article->title, 'text' => $article->text, 'author' => $article->author );
$this->form = new ArticleEditForm( $defaults);
这有效。我的问题是,为了简单起见,上面的“文章”只是一个例子。事实上,我的表单大约有 30 个字段。因此,此解决方案有效的唯一方法是必须在数组中手动指定 30 个单独的默认值。显然,从维护的角度来看,这是一个糟糕的解决方案。
我认为 Symfony 开发人员足够聪明,能够想出一个好的解决方案,但我就是找不到它......有任何线索吗?
My action:
public function executeEdit(sfWebRequest $request)
{
// Get the object
$article = $this->getRoute()->getObject();
// Create the form
$this->form = new ArticleEditForm( $article );
}
You can see that $article
is the Doctrine Collection that gets passed to the Form as default values. The $article
object contains a fields like "title", "text", "author", etc.
But this causes an error in the Form creation:
500 | Internal Server Error | Doctrine_Record_UnknownPropertyException
Unknown record property / related component "_csrf_token" on "article"
So basically, the form is trying to use the Doctrine Collection to fill out default values for the form elements. But there obviously isn't a csrf_token
in that object... But it's still trying to find one to use as the default value...
And what happens if you have a form where there are always extra empty fields that are empty but others have default values that are passed. If those empty fields don't have set values in the Doctrine Collection then you get an error...
Now, obviously I could just make a simple array ahead of time where I specify the default values and pass that:
$defaults = array( 'title' => $article->title, 'text' => $article->text, 'author' => $article->author );
$this->form = new ArticleEditForm( $defaults);
This works. My problem is that the above "article" is an example for simplicity reasons. In reality my form has about 30 fields in it. So the only way that this solution would work is to have to manually specify 30 individual default values in an array. This is a poor solution from a maintenance point-of-view, obviously.
I figure the Symfony developers are smart enough to come up with a good solution but I just can't find it... Any clues?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个问题是因为你的表单没有创建_csrf_token。您可以在模板页面中创建表单时添加它,例如:
This problem is all about your form doesn't create _csrf_token. You can add it when creating form in your template page like:
我发现了我的问题。我的自定义表单正在扩展
BaseForm
。一旦我更改了它以扩展BaseArticleForm
,我就能够成功地将文章 Doctrine Collections 作为默认值传递。I discovered my problem. My custom form was extending
BaseForm
. Once I changed this to extendBaseArticleForm
then I was able to successfully pass article Doctrine Collections in as default values.