将值预加载到 symfony 表单中的字段中,出现错误“无法更新表单字段”
我有一个表单,应该在数据库中创建一个非常简单的新条目。该表中的一个字段与另一表相关。具体来说,有一个人们参加的活动,我需要将人员分配到该活动。每个事件可以有几个人参加,并且可以有多个事件。
当管理员用户将成员添加到活动时,我不需要他们在实际表单上选择活动,这应该通过 URL 传递。例如 event/new/id/1
但是,我真的很苦恼如何在表单中包含该 ID。如果我尝试手动设置字段值,则会收到错误无法更新表单字段
例如,
$this->['attendanceSuccess_id'] = 1;
如果我尝试隐藏该字段:
$this->widgetSchema['attendanceSuccess_id'] = new sfWidgetFormInputHidden();
表单显示,但显然没有传递任何值,并且我得到“该字段是必需的错误”
这似乎是一个非常简单和常见的事情,但我找不到任何解决方案!如何将 URL 值传递到字段指向另一个类的表单中?
I have a form that is supposed to create a very simple new entry into the database. One of the fields in this table is related to another table. Specifically, there is an event that people attend, and I need to assign people to an event. Each event can have several people attending, and there can be several events.
When an admin user adds members to an event, I don't need them selecting the event on the actual form, this should be passed via the URL. e.g.event/new/id/1
However, I am really struggling as to how to include that ID in the form. If I try and manually set the field value, I get the error Cannot Update Form Fields
e.g.
$this->['attendanceSuccess_id'] = 1;
If I try and hide the field:
$this->widgetSchema['attendanceSuccess_id'] = new sfWidgetFormInputHidden();
The form shows but obviously no value is passed and I get "that fields is required error"
This seems like a really simple and common thing to do, but I can't find any solutions! How can I pass a URL value into a form where the field points to another class?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这就是我所做的。基本上,您的表单包含一个隐藏的 id,您的操作从请求中获取它并作为选项放入表单中。在表单中您还可以设置与 Doctrine 对象的关系。
你的表单应该有类似的内容(我不知道你的表单应该精确扩展什么,因为你使用的是 Doctrine,而我的是 Propel)
然后你的操作说:
Here's what I've done. Basically your form holds a hidden with the id, and your action gets it from the request and puts in into the form as an option. In the form you can also set the relationship to the Doctrine objects.
Your form should have something like (I don't know what your form should extend exactly since you are using Doctrine and mine was in Propel)
Then your action says: