【发布时间】:2012-11-26 14:35:14
【问题描述】:
我在我的 php 项目中使用 symfony 1.4。我有一个可以根据用户配置进行扩展的表单。如果用户为任务选择了额外的标签或预算,则该信息应该嵌入到表单类中,并在表单部分(附加输入)中显示为小部件。
例如:
管理员希望只执行基本任务而没有 buget 或标签。编辑或创建新任务时仅显示 TasksForm 小部件。
管理员希望执行带有标签的基本任务。 TasksForm 显示附加字段,使用户能够选择标签。
管理员希望拥有所有选项(标签 + 预算)
db 中的关系是:
Tasks:
columns:
name: string(127)
created_at: timestamp
updated_at: timestamp
Budget:
columns:
task_id: integer
budget_prognosis: integer
budget_used: integer
relations:
Tasks:
local: task_id
foreign: id
type: one
TaskHasLabel:
columns:
task_id:
type: integer
primary: true
label_id:
type: integer
primary: true
relations:
Tasks:
local: task_id
foreign: id
type: one
onDelete: CASCADE
Labels:
local: label_id
foreign: id
type: one
onDelete: CASCADE
Labels:
columns:
name: string(127)
relations:
Tasks:
class: Task
local: label_id
foreign: task_id
refClass: TaskHasLabel
目标是在未来轻松更改表单配置并添加新的配置选项。我知道我可以通过为每个组合创建单独的表单来做到这一点,但我不想重复自己。另一个想法是将所有字段组合在一个表单中并隐藏它们,但在我看来这不是一个好主意。我想到了通过表单构造函数中的参数进行配置,但我也不确定。在这种情况下,最佳做法是什么?
【问题讨论】:
-
想办法从 Symfony 中生成这样的自定义表单将大大增加复杂性(并且可能会破坏 MVC 结构)。我会使用 JS 隐藏表单元素,使用 cookie 存储配置,处理浏览器中的所有表单自定义。
标签: php forms configuration symfony-1.4