0

when creating a fairly complicated form that will be used to edit a database record I am currently loading the controller, sending the request to the model to make the form and then loading the output from the model itself instead of sending the form as a $string to the view to insert into the page.

I keep getting hung up on whether we should create the form (which needs to access the database even to create the form because it's dynamic) in the model or in the view and if it's created in the model, should we create it as a string and send it to a view.

I know there is no "perfect" answer here, but we are trying to standardize so that as we bring on developers, we've done things in an industry-standard way (which we are still learning). Using the Code Igniter framework.

2 Answers 2

8

In my mind, there is a perfect answer here:

The Model handles the data side of things. Displaying a form to edit the data in the Model is the responsibility of the UI which is the responsibility of the View.

In other words the Model shouldn't render it's own form. The View should be doing that.

Sign up to request clarification or add additional context in comments.

8 Comments

so what if you have a completely dynamic form (where the database determines what fields should appear, what labels should appear, what the styling is like, etc.), then you are having to use the database to create an element that is being viewed.
I'd argue that any sufficiently complicated view is going to have some limited logic inside of it. In your case, I would expect that there may be some if() statements in a view to determine what fields/data to display.
@sfrench - I would argue that, unless you're talking about basic control structures, you're wrong. In that case I would steal the ViewModel concept from ASP.NET MVC and put the logic in that object.
Your view is expected to do simple logic like if and some simple for loops. You could put all of the form fields into an array and then pass the array to the view, which can then loop over the array and display each field.
I dont know about CI but in Both Zend and Symfony a Form is an object that contains Field and Validator instances. The form is instantiated in the controller and then passed to the view. The View then has helpers that know how to render/decorate the Field objects attached to the Form. In my mind you should always be doing something to this effect (ie. using decorators and/or view helpers) Not directly attaching strings representing html tags to a model or assigning them directly to view variables.
|
2

Don't limit your application by determining that the View should not contain any server side code. If you were to pass an array to the View then you would need to loop through in order to output this data to the browser. By creating the string in the Model and passing it to the View, you must be creating a lot of HTML in the Model which is what the View is for. I have yet to come across a situation where I have not been able to avoid using at least a little PHP in a form, usually to add the value from the POST array to an input form where it has failed validation.

Use the method that seems the cleanest and most sensible to you and don't beat yourself up you are bending the rules - in this case you aren't. As long as your application is secure and it works with clean, readable and maintainable code then run with it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.