symfony 1.4 条令形式 - 引入固定值字段
编辑。总结:我只想要 SomeForm 扩展我的 DoctrineForms 以不包含某些字段。它们不可编辑。我想在代码中的某处设置固定值。希望这应该是足够的信息,您不需要阅读本文的其余部分......
您好。这是我的情况:
- 我有一些由学说生成的模型,
- 我在前端和后端应用程序中都有该模型的 CRUD 屏幕
- 这两个 CRUD 屏幕之间的唯一区别(除了审美差异)是在前端,一个特定的字段是固定的。即管理员可以根据需要更改该值,但普通用户则不能。该值只是我在代码中定义的常量。该字段不应显示在前端添加/编辑屏幕中。
我想知道的是,“正确”的方法是什么?我可以想出几种方法来破解它,但无论我做什么都感觉像是一个尴尬的解决方法。
是否有任何正确的方法可以扩展我的 Form 类(BaseFormDoctrine)或其他合适的地方?
编辑:正如下面的评论所指出的,我实际上使用的是原则:生成模块,这显然与“CRUD”不同。
另外:虽然我还没有理想地解决这个问题,但我想我知道解决方案在哪里,我只需要更深入地研究 symfony 形式: http://www.symfony-project.org/forms/1_4 /zh/02-表单验证
EDIT. TO SUMMARISE: I just want SomeForm that extends my DoctrineForms to NOT INCLUDE certain fields. They are NOT editable. The fixed value I want to set in the code somewhere. Hopefully this should be enough info that you don't need to read the rest of this post...
Hi there. Here is my situation:
- I have SomeModel generated by doctrine
- I have CRUD screens for this model in both my frontend and backend apps
- The only difference between these two CRUD screens (apart from aesthetic differences) is on the frontend, one particular field is FIXED. i.e. administrators can change that value if they want, but regular users can not. The value will just be a constant I define in the code. This field should not be displayed in the frontend add/edit screens.
What I want to know is, what is the "correct" way to do this? I can think of a few ways to just hack it in, but whatever I do feels like an awkward workaround.
Is there any proper way to do this extending my Form classes (which BaseFormDoctrine) or somewhere else appropriate?
EDIT: As pointed out in the comments below I am actually using doctrine:generate-module which is different to "CRUD" apparently.
ALSO: While I still haven't solved this ideally, I think I know where the solution lies, I just have to dig deeper into symfony forms:
http://www.symfony-project.org/forms/1_4/en/02-Form-Validation
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 /lib/form 文件夹中创建另一个表单来扩展您的普通表单,然后覆盖相应的字段。以下将从表单中删除该字段,以便根本不显示该字段。
或者,如果您想要呈现该值,但不允许对其进行编辑,您可以执行以下操作:
然后创建一个 sfWidgetFormPlain 小部件,该小部件仅输出该值并将其粘贴到 symfony 可以找到的地方它(lib / form / widget或其他东西)。
然后,您可以使用此表单而不是普通表单来编辑您不希望编辑的表单。检查 symfony 文档以了解如何执行此操作,具体取决于您是在模块中还是通过管理生成器显示它。
Create another form in your /lib/form folder that extends your normal form and then override the appropriate field. The following will remove the field from the form, so that it isn't displayed at all.
Or if you were wanting to render the value, but not allow it to be edited, you could do the following:
And then create a
sfWidgetFormPlain
widget that just outputs the value and stick it somewhere that symfony can find it (lib/form/widget or something).You then use this form rather than your normal one for the one that you don't want to be able to edit. Check the symfony docs for how to do this, depending on whether you are showing it in a module, or through the admin generator.