Welcome Guest, Not a member yet? Register   Sign In
Reusing the same form code for creating and editing
#1

[eluser]Unknown[/eluser]
If possible, I would like to write my form code only once and use that code for both creating new objects and for updating existing objects. Repopulating the form after a submission is easy using the validation framework. For both edit and create, I just do this:

Code:
<input name="name" id="workshop_name" value="<?= $this->validation->name ?>" />

I saw an approach for reusing the form after validation and for editing where the code looked like this:

Code:
<input name="name" id="workshop_name" value="<?= ($this->validation->name) ? $this->validation->name : $workshop->name ?>" />

The question is how to also use that form when I'm creating a new object. I could either create a stubbed out workshop object so that the same code that works for presenting the update form works for presenting the new form. Or I could leave out the workshop object entirely and put more logic into the view to present an empty form in the case where we're presenting something new.

Is there an established pattern for dealing with this case?

In many frameworks, you create a form backing object that you can instantiate when you're presenting the form for creation, populate from the database in cases where you're editing, and populate from the request when you're validating. Is that any approach anybody uses with CodeIgniter?


Messages In This Thread
Reusing the same form code for creating and editing - by El Forum - 06-24-2007, 11:42 AM
Reusing the same form code for creating and editing - by El Forum - 06-24-2007, 01:52 PM
Reusing the same form code for creating and editing - by El Forum - 06-24-2007, 06:58 PM
Reusing the same form code for creating and editing - by El Forum - 10-01-2007, 10:25 AM
Reusing the same form code for creating and editing - by El Forum - 10-01-2007, 11:56 AM
Reusing the same form code for creating and editing - by El Forum - 10-01-2007, 12:11 PM



Theme © iAndrew 2016 - Forum software by © MyBB