I use symfony 1.4 in my php project. I’ve got a form which might be extended depending on user configuration. If user chooses additional labels or budget for tasks, this information ought to be embedded to form class and displayed as widget in form partial (additional inputs).
For example:
-
Admin wants to have only basic tasks without buget or labels. Only TasksForm widgets are displayed when editing or creating new task.
-
Admin wants to have basic tasks with labels. TasksForm is displayed with additional field that enables user to choose labels.
-
Admin wants to have all options (labels + budget)
Relations in db are:
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
Aim is to easily change configuration of forms and add new configuration options in the future. I know I could do this by creating separate form for every combination, but I don’t want to repeat myself. Another idea is to combine all fields in one form and hide them, but it doesn’t seem to me like a good idea. I thought of configuration via parameters in form constructor, but I’m not sure of that either. What’s the best practice in this situation?
Of all the possibilities:
Embedded forms work quite well in SF1.4 (see here). This would be my solution. It also keeps the MVC working. If you are using Doctrine for your model, you can also read this.