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?
#2

[eluser]Rick Jolly[/eluser]
I just live by this:

If the form has not been posted, then prepopulate the validation object.

That way, for example,
Code:
value="<?= $this->validation->name; ?>
will always have a value and your views will be cleaner.

This will work for editing or adding an object. So if you are editing there will be an object id sent to the controller. Otherwise assume you are adding an object and just initialize the validation object with blank and/or default field values.
#3

[eluser]marcoss[/eluser]
I use a custom helper function that performs a various checks, it takes two arguments: field($key = '', $data = '').

It searches for the key in the post array and if it finds the key it will return the value, if there is no such a key in the request then it looks for the same key (ot property) in the $data (array|object), if that doesn't work either it will just return and empty string.
#4

[eluser]ericsodt[/eluser]
Sounds like something I am looking for... could you post the code?

Thanks
#5

[eluser]marcoss[/eluser]
The code is under the GPL

It is just a helper function, no security checks are performed, they are supposed to be done prior the data is used, and of course it is very primitive, so any enhancement is welcomed.

Code:
function field($key = '', $data = '') {
    
        global $CI;
        
        if (is_object($data) && property_exists($data, $key)) :
            $value = $data->$key;
        elseif (is_object($data) && !property_exists($data, $key)) :
            $value = '';
        else:
            $value = $data;
        endif;
        
        $post = $CI->input->post($key);
                
        if($post) :
            return $post;
        elseif ( ($value) && !empty($value) ) :
            return $value;
        else :
            return '';
        endif;
    }
#6

[eluser]ericsodt[/eluser]
[quote author="marcoss" date="1191279371"]The code is under the GPL

It is just a helper function, no security checks are performed, they are supposed to be done prior the data is used, and of course it is very primitive, so any enhancement is welcomed.

Code:
function field($key = '', $data = '') {
    
        global $CI;
        
        if (is_object($data) && property_exists($data, $key)) :
            $value = $data->$key;
        elseif (is_object($data) && !property_exists($data, $key)) :
            $value = '';
        else:
            $value = $data;
        endif;
        
        $post = $CI->input->post($key);
                
        if($post) :
            return $post;
        elseif ( ($value) && !empty($value) ) :
            return $value;
        else :
            return '';
        endif;
    }
[/quote]

Thanks for posting, are there any examples of how to use, for a point of reference? or should I extend the validation class and implement over write the existing field method




Theme © iAndrew 2016 - Forum software by © MyBB