Welcome Guest, Not a member yet? Register   Sign In
Sharing a view file for Add and Edit functions
#1

[eluser]digitalbloke[/eluser]
Good evening.

I have a controller with two functions. One to add a news article and another to edit. Currently each function has a separate view file. Keeping DRY in mind I'm trying to replace the two view files with one. Where I'm getting stuck is with the set_value() function. As obviously for the add view the second parameter of set_vaule() has not been set and therefore causes php to show an "Undefined variable" error.

I could do something like this, but sometimes it gets a bit long and messy:
Code:
<?php echo set_value('title', ( isset($title) ) ? $title : ''); ?>

I've also come across this wiki article:
http://codeigniter.com/wiki/Add_Edit_Views/

But I'm not too keen on using the @ symbol to suppress php errors and was wondering if anyone could point me in the direction of a better way to solve this problem.

Thanks in advance.

Smile
#2

[eluser]Tim Brownlaw[/eluser]
Hi,

Well an Add/Edit form has a lot of common features.
As an example - let's say it had a title text input and an article textbox.

The input fields are always common for both an add and edit.
What does change is
1. The title - ie - "Add an Article" and "Edit an Article" to appear in the form.
2. The form action - add and edit. So you can redirect it to the appropriate method.
3. The buttons - "Add" and "Update" / "Reset"( reloads the form in case of a whoopsie - optional )

I've not got time to go into this in more detail as I've got to head out. But hopefully that might get you thinking along the right track.

So basically the form view is very simple and you just need to alter the Title, action and buttons which you can define in
the controller.

I'm also a stickler for writing my form views the ole fashioned way in straight HTML. I makes more sense.

Cheers
Tim
#3

[eluser]digitalbloke[/eluser]
Hi Tim

Thanks for your reply. I already have the titles and button text working. The problems I'm having are with setting the initial value of the text fields for the edit view without getting php errors in the add view. Sorry maybe I didn't make that 100% clear. My view looks something like:
Code:
<?php echo form_open('article/save'); ?>
    <fieldset>
        <legend>&lt;?php echo $form_title; ?&gt;</legend>
        <ol>
            <li><label for="title">Title:</label>
            &lt;input type="text" name="title" id="title" value="&lt;?php echo set_value('title', ( isset($title) ) ? $title : ''); ?&gt;" /&gt;&lt;/li>
            <li><label for="story">Story:</label>
            &lt;textarea name="story" id="story"&gt;&lt;?php echo set_value('story', ( isset($story) ) ? $story : ''); ?&gt;&lt;/textarea&gt;&lt;/li>
        </ol>
    </fieldset>
    &lt;input type="submit" name="save" id="save" value="&lt;?php echo $button_title; ?&gt;" /&gt;
&lt;/form&gt;
Thats trimmed down, and may even contain errors.


Thanks



t.
#4

[eluser]Matthieu Fauveau[/eluser]
Hi digitalbloke,

What I do is that I pass all the vars to the view whether it's in edit mode or not. If it's in edit mode they are filled up, otherwise they are just set to ''.

I believe it's more efficient than calling an isset() on each fields Wink
#5

[eluser]billmce[/eluser]
[quote author="digitalbloke" date="1270523165"]
&lt;input type="text" name="title" id="title" value="&lt;?php echo set_value('title', ( isset($title) ) ? $title : ''); ?&gt;" /&gt;&lt;/li>


[/quote]

You don't need the 'isset' ... you may need to initialize your values before the view.
In other words, blank all the values, set what needs set, then bring up the view.
#6

[eluser]digitalbloke[/eluser]
Hi Matthieu Fauveau and billmce

Thanks for your replies. I think this is the way to go Smile Should keep me busy for a few hours!




Theme © iAndrew 2016 - Forum software by © MyBB