Welcome Guest, Not a member yet? Register   Sign In
help: losing uri segment when failing form validation
#1

[eluser]pabloheim[/eluser]
Hi

first of all , sorry for my english Smile

im having some trouble with the process of editing an item which id is taken from a uri segment. all is fine if you dont commit any mistake. if form validation fails, the page is reloaded but this time without the uri element corresponding to the id.

Here is an example of the situation:

Code:
function edit(){
        
        $this->load->library('form_validation');
        $this->form_validation->set_rules('userfile', 'User file', 'callback_check');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        
        if ($this->form_validation->run() == FALSE){ // if it fails , load the page again
        
            $data['title'] ='Edit';
            $data['page'] ='edit';
            $data['id_item'] =$this->uri->segment(3,0); // the id of the item . it is passed to hidden input text
            $data['info'] =$this->Item_model->get('file_name',$this->uri->segment(3,0)); // data to be displayed within the form
            $this->load->vars($data);
            $this->load->view('contenedor');
        }
        else{ // if  ok , insert data through Item_model and then redirect
        
            if($this->Item_model->edit()) {
            $this->session->set_flashdata('message', '<div class="success">All is ok! </div>');
            redirect('index','location');
            }
            else echo 'Error';    // problem inserting data
            
    
        }
    }

Ok, so when i enter into the edit page , the url is like:

Code:
localhost/app/index.php/controller/edit/4
where "4" is the id.

If you submit the form and valdation fails, all is reloaded but the url is like:

Code:
localhost/app/index.php/controller/edit

So the id is lost and there is no reference to the element that i wanted to edit.

The first solution was to keep the id in a session, with is code:

Code:
function edit(){
        
        $this->load->library('form_validation');
        $this->form_validation->set_rules('userfile', 'User file', 'callback_check');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        
        if ($this->form_validation->run() == FALSE){ // if it fails , load the page again
//////MODIFICATION: save the id in session variable        
if($this->uri->segment(3,0) != '0') $this->session->set_userdata('id_item',$this->uri->segment(3,0));
//////
            $data['title'] ='Edit';
            $data['page'] ='edit';
            $data['id_item'] = $this->session->userdata('id_item'); //MODIFICATION the id of the item . it is passed to hidden input text
            $data['info'] =$this->Item_model->get('file_name',$this->session->userdata('id_item');); // data to be displayed within the form
            $this->load->vars($data);
            $this->load->view('contenedor');
        }
        else{ // if  ok , insert data through Item_model and then redirect
        
            if($this->Item_model->edit()) {
            $this->session->set_flashdata('message', '<div class="success">All is ok! </div>');
$this->session->unset_userdata('id_item'); // MODIFICATION: liberate after use
            redirect('index','location');

            }
            else echo 'Error';    // problem inserting data
            
    
        }
    }


this works, but i think its a little bit complex.
please, can you suggest another way to do this??

Thanks in advance!!
#2

[eluser]WanWizard[/eluser]
If the URL is different upon submitting a form, check the form action in your page source. Does it contain the '4' or is it already stripped there?
#3

[eluser]pabloheim[/eluser]
[quote author="WanWizard" date="1278796962"]If the URL is different upon submitting a form, check the form action in your page source. Does it contain the '4' or is it already stripped there?[/quote]

Hi, thanks for comment.

in the form, the url in the action attribute the same asthe controller

Code:
&lt;?php echo form_open('controller/edit');?&gt;

as you can see, it doesnt contain the id.
im putting it in a hidden input :

Code:
&lt;?=form_hidden('id_item',$id_item)?&gt; //comes from the $data['id_item'] in the controller
then manage all in the model to insert new information
#4

[eluser]Flemming[/eluser]
what I said was a load of rubbish so I deleted it!
#5

[eluser]Flemming[/eluser]
OK second attempt!

At the very top of your edit() method, above the validation IF, put the $id into a variable like this:

Code:
$edit_id = $this->uri->segment(3);
if(! $edit_id) $edit_id = $this->input->post('id_item);
// if there's still no ID, redirect somewhere safe
if(! $edit_id) redirect('some_controller');

that should solve your problem I think
#6

[eluser]WanWizard[/eluser]
[quote author="pabloheim" date="1278797691"]in the form, the url in the action attribute the same asthe controller
Code:
&lt;?php echo form_open('controller/edit');?&gt;
as you can see, it doesnt contain the id. im putting it in a hidden input :
Code:
&lt;?=form_hidden('id_item',$id_item)?&gt; //comes from the $data['id_item'] in the controller
then manage all in the model to insert new information[/quote]
So it's quite logical that after you submit the form the id is no part of the URI, since you didn't include it.

You have to retrieve it from the posted form. I recommend not to use $this->input->post(), but to validate it properly using the form validation library, and after validation use set_value() to access the ID variable.




Theme © iAndrew 2016 - Forum software by © MyBB