Welcome Guest, Not a member yet? Register   Sign In
Editing a form - CI strategy
#1

[eluser]stevefink[/eluser]
Hi all,

So I have a form where a user inputs their stuff, etc. Later on, this data gets populated into a table with zebra stripes, nothing out of the ordinary there.

Now I have the issue, where I need to have the user be able to edit the form and update the db, versus insert data. Inputs that would normally get validated as FALSE in the insert form, might not be false in this particular mode. For instance, if a key that requires to be unique needs to be changed in the Edit mode, it should be allowed to get changed. In insert mode, it would give the user an error that the key has already been added.

I've been searching the threads without much luck on how folks tackle this problem using CI. Would anyone be able to suggest anything? Should I use a completely separate controller for modifying the information versus adding the information? Use the same view by the different controllers so everything visually and input wise stays the same?

Thanks so much, could really use a helping hand here.

- sf
#2

[eluser]Scott - Beyond Coding[/eluser]
[quote author="stevefink" date="1187856547"] Would anyone be able to suggest anything? Should I use a completely separate controller for modifying the information versus adding the information? Use the same view by the different controllers so everything visually and input wise stays the same?[/quote]

I personally use a common view that contains just the form for both inserts and edits. Then I have controllers set up such as the following (an example being inserting/editing user details):

mysite.xxx/user/new (to insert)
mysite.xxx/user/edit/47 (to edit a user with ID=47)

If the validation rules need to be different between new records or editing records, you can simply test the 2nd segment of the URI to vary the rules.

You can also use test the 2nd segment of the URI from the view itself to add/remove certain form elements depending on whether the URI is for a 'new' record or to 'edit' an existing record.
#3

[eluser]Phil Sturgeon[/eluser]
[quote author="Goo Theory" date="1187864279"]... you can simply test the 2nd segment of the URI to vary the rules.

You can also use test the 2nd segment of the URI from the view itself to add/remove certain form elements depending on whether the URI is for a 'new' record or to 'edit' an existing record.[/quote]

That's one way but if you find you are making way to make if statements to check if its add or edit, try using totally different methods. You will find yourself using some repeated code but as time goes on you will find it more manageable as you can add in bits and take bits out without worrying about the affect on the other.

You can still use the same view doing it this way. I just output a variable like:

Code:
$data['form_action'] = 'add';
// or
$data['form_action'] = 'edit/'.$itemID;
#4

[eluser]Scott - Beyond Coding[/eluser]
[quote author="thepyromaniac" date="1187883484"]That's one way but if you find you are making way to make if statements to check if its add or edit, try using totally different methods. You will find yourself using some repeated code but as time goes on you will find it more manageable as you can add in bits and take bits out without worrying about the affect on the other.

You can still use the same view doing it this way. I just output a variable like:

Code:
$data['form_action'] = 'add';
// or
$data['form_action'] = 'edit/'.$itemID;
[/quote]Good call on using the same view with different actions Pyro Smile

Funnily enough I usually do use separate controllers for add/edit but lately I've found it a real pain when doing updates. What I mean is, say I want to add an extra field to a form... That should be a very quick process but I end up having to change code in so many places and sometimes forget to change the code in one area or another (which gets picked up in testing, but is still frustrating).

Lately I've been experimenting to try to move toward defining data in a single area and reusing that throughout. Like you say though, it can get messy with all the conditional testing.. Smile
#5

[eluser]NemetraL[/eluser]
[quote author="thepyromaniac" date="1187883484"]
That's one way but if you find you are making way to make if statements to check if its add or edit, try using totally different methods. You will find yourself using some repeated code but as time goes on you will find it more manageable as you can add in bits and take bits out without worrying about the affect on the other.

You can still use the same view doing it this way. I just output a variable like:

Code:
$data['form_action'] = 'add';
// or
$data['form_action'] = 'edit/'.$itemID;
[/quote]

Personnally I prefer to create only one edit function that will add a new item if called like that:

Code:
http://www.website.com/controller/edit

... and edit a specific item (here: id 42) if called like that:

Code:
http://www.website.com/controller/edit/42

Inside the controller, a simple if() statement can distinguish between the two cases by analyzing $this->uri->segment(3);

In the model, either I create two function, or one with the same if() calling either a
Code:
SQL INSERT INTO
or a
Code:
SQL UPDATE

For basic CRUD, that's incredibly time-saving.

@stevefink: in your particular case, you will need the two model functions and you will do different checkings in each one of them. This way you'll keep a common controller, a common view and, still, two different behaviours.
#6

[eluser]Phil Sturgeon[/eluser]
[quote author="NemetraL" date="1187993931"]Personnally I prefer to create only one edit function that will add a new item if called like that:

Code:
http://www.website.com/controller/edit

... and edit a specific item (here: id 42) if called like that:

Code:
http://www.website.com/controller/edit/42

Inside the controller, a simple if() statement can distinguish between the two cases by analyzing $this->uri->segment(3);

In the model, either I create two function, or one with the same if() calling either a
Code:
SQL INSERT INTO
or a
Code:
SQL UPDATE

For basic CRUD, that's incredibly time-saving.

@stevefink: in your particular case, you will need the two model functions and you will do different checkings in each one of them. This way you'll keep a common controller, a common view and, still, two different behaviours.[/quote]

Its just one of those things that can vary on the situation, there's no right or wrong way. Anyway no time lost by typing it out once then ctrl + v -> tweak.
#7

[eluser]NemetraL[/eluser]
Sure..

But as a matter of fact, it's better not to repeat the same code when we're sure it will remain strictly the same. That's why I restricted the scope to "basic CRUD" ;-).
#8

[eluser]Phil Sturgeon[/eluser]
Obviously, hence the comment:

Quote:Its just one of those things that can vary on the situation, there’s no right or wrong way.




Theme © iAndrew 2016 - Forum software by © MyBB