Symfony:如何隐藏表单字段的显示,然后在操作类中为其设置值
我对 symfony 相当陌生,我有 2 个与我的表“Pages”相关的字段;创建者和更新者。这些与用户表 (sfGuardUser) 作为外键相关。我希望这些字段从编辑/新表单中隐藏,因此我已将生成器.yml 文件设置为不显示这些字段:
form:
display:
General: [name, template_id]
Meta: [meta_title, meta_description, meta_keywords]
现在我需要在保存时设置字段。我一整天都在寻找如何做到这一点,并尝试了一百种方法。我在 actions 类中使用的方法是这样的:
protected function processForm(sfWebRequest $request, sfForm $form)
{
$form_params = $request->getParameter($form->getName());
$form_params['updated_by'] = $this->getUser()->getGuardUser()->getId();
if ($form->getObject()->isNew()) $form_params['created_by'] = $this->getUser()->getGuardUser()->getId();
$form->bind($form_params, $request->getFiles($form->getName()));
所以这有效。但我感觉理想情况下我不应该修改网络请求,而是直接修改表单/对象。然而,我在以下方面没有取得任何成功:
$form->getObject()->setUpdatedBy($this->getUser()->getGuardUser());
如果有人能够就解决此类问题的最佳方法提供任何建议,我将非常感激。
谢谢, 汤姆
I am fairly new to symfony and I have 2 fields relating to my table "Pages"; created_by and updated_by. These are related to the users table (sfGuardUser) as foreign keys. I want these to be hidden from the edit/new forms so I have set up the generator.yml file to not display these fields:
form:
display:
General: [name, template_id]
Meta: [meta_title, meta_description, meta_keywords]
Now I need to set the fields on the save. I have been searching for how to do this all day and tried a hundred methods. The method I have got working is this, in the actions class:
protected function processForm(sfWebRequest $request, sfForm $form)
{
$form_params = $request->getParameter($form->getName());
$form_params['updated_by'] = $this->getUser()->getGuardUser()->getId();
if ($form->getObject()->isNew()) $form_params['created_by'] = $this->getUser()->getGuardUser()->getId();
$form->bind($form_params, $request->getFiles($form->getName()));
So this works. But I get the feeling that ideally I shouldnt be modifying the web request, but instead modifying the form/object directly. However I havent had any success with things like:
$form->getObject()->setUpdatedBy($this->getUser()->getGuardUser());
If anyone could offer any advice on the best ways about solving this type of problem I would be very grateful.
Thanks,
Tom
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
处理并保存表单后,您可以在对象上设置这些字段并重新保存:
还有一个名为 Blameable 的 Doctrine 扩展,可以自动在指定模型上设置edited_by 和created_by 字段。 Doctrine 网站正在进行一些重组,但这里是 扩展程序的缓存页面。
After processing and saving the form you could set those fields on the object and re-save:
There's also a Doctrine extension called Blameable that automatically sets edited_by and created_by fields on specified models. The Doctrine website is undergoing some reorganization but here is the cached page for the extension.
要处理表单,请创建一个新对象,设置字段然后保存。
To process your form create a new object, set the fields then save.
您想要做的是自定义表单并在配置中取消设置表单的“created_at”和“updated_at”部分
然后它们不会显示在表单中,并在保存之前通过“Timestampable”行为获取设置的值
http://stereointeractive.com/blog/ 2010/04/07/symfony-forms-hide-created_at-updated_at-columns/
What you want to do is customize your form and unset the 'created_at' and 'updated_at' pieces of the form in configure
Then they won't show up in the form and will get the values setup by the "Timestampable" behavior before being saved
http://stereointeractive.com/blog/2010/04/07/symfony-forms-hide-created_at-updated_at-columns/