I have an MVC that serves a form to a view. I'm pondering the best approach to serving form content, that is form elements such as labels and form inputs such as selects. It is possible that the form elements will change often and will have many different versions spanning several pages.
I have thought about three approaches, each unsatisfactory to me.
Pros: If I need a particular set of elements, I can call a view with one line of code from my controller.
Cons: I will end up with dozens upon dozens of views.
array('select' => array('label' => "element 1", 'class => "big"), 'select', 'input', 'input')
, which will take the array and parse it into the markup as form elements.Pros: Allows me to have just one view, and I can specify the set of form elements I want loaded into the view at the controller level.
Cons: My controller will go from several lines of code to several hundred lines of array definitions pretty quick.
Pros: Several lines of code at my controller and one view file.
Cons: Making changes to any one set of form elements becomes a real pain. Anytime I have to include a new set or make changes, I have to access the database and unserialize and serialize.
So for templating different versions of the same form, and also allowing interchangeability of form elements, what is the approach do you guys suggest?
You could build a library of form methods and extend the appropriate controllers. I will usually build libraries or "helper" classes for any perpetual data, keeping my controllers uniform and tidy. Doing so gives you the ability to simply extend a controller to class Form_Data
where you could have dedicated methods that build individual form elements, also enforcing separation of concerns.