Welcome Guest, Not a member yet? Register   Sign In
property of non-object
#1

[eluser]haris244808[/eluser]
i have an anchor tag like: href=\"".base_url()."file/edit_file/".$results->id."\>Click</a>

where i redirect users to a specific page using this model:

Code:
function edit_file(){

  $data['page_title'] = 'Edit File';
  $data['content'] = 'edit_file_view';

//from here i get the file details depending on the id
  $file_id = $this->uri->segment(3) ? $this->uri->segment(3) : 0;
  $data['file_results'] = $this->files_model->select_file_by_id($file_id);

  $this->load->view('includes/template', $data);
}


then in the view i user $file_results->'field form the db' to echo out the data...and it works ok

in this view i have an update button that if anychange happens it will update those fields..
here is the method where i validate fields:
Code:
function validate_file_edit(){

  $file_id = $this->uri->segment(3) ? $this->uri->segment(3) : 0;

  $this->form_validation->set_error_delimiters('<div class="validation_errors">×', '</div>');
   $this->form_validation->set_rules('case_nr', 'Case Nr.', 'trim|required|max_length[50]');
   $this->form_validation->set_rules('subject', 'Subject', 'trim|required|max_length[50]');
   $this->form_validation->set_rules('content', 'Content', 'trim|required');
   $this->form_validation->set_rules('description', 'Description', 'trim|max_length[1000]');
   $this->form_validation->set_rules('priority', 'Priority', 'trim|required');
  
   if ($this->form_validation->run() == TRUE) {
    
    $result = $this->files_model->update_file($file_id);

    if ($result == TRUE) {
    
     $this->session->set_flashdata('file_upd_succ', 'File has been updated successfully');
     redirect('home');
    }
    else{

     $this->session->set_flashdata('file_upd_fail', 'File failed to be updated');
     redirect('file/edit_file');
    }
   }
   else{

    $this->form_validation->set_error_delimiters('<div class="validation_errors">×', '</div>');
    $this->session->set_flashdata('validation_errors', validation_errors());
    redirect('file/edit_file');
   }
}


as you can see in case of validation fail i redirect to the same page again showing a message with the help of flashdata...however when redirected to that page...it shows the validation error but it gives this error for every field:
Trying to get property of non-object

as i understan it is because when it is redirected it doesnt take the file_id and selects data...

but HOW CAN I SOLVE THIS ISSUE???
#2

[eluser]TheFuzzy0ne[/eluser]
What's with all the redirecting? You should be doing all of your validation in the edit_file() method, and then redirecting when it passes. Also, I doubt you're loading the validation library.

Look at the tutorial [url="http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#tutorial"]here[/url]. This is what your application flow should be. When you redirect, you lose your $_POST data.
#3

[eluser]rana[/eluser]
can you please share your view file? I guess, in fail case, you trying to show input values on views. But you don't have defined them here. and thus such problem can occur easily. Also, as thefuzzyone said, you should rethink about your workflow.
#4

[eluser]haris244808[/eluser]
[quote author="TheFuzzy0ne" date="1364459338"]What's with all the redirecting? You should be doing all of your validation in the edit_file() method, and then redirecting when it passes. Also, I doubt you're loading the validation library.

Look at the tutorial [url="http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#tutorial"]here[/url]. This is what your application flow should be. When you redirect, you lose your $_POST data.[/quote]

here is what i came up with: but still getting the same error
Code:
function edit_file(){

  $data['page_title'] = 'Edit File';
  $data['content'] = 'edit_file_view';

  $file_id = $this->uri->segment(3) ? $this->uri->segment(3) : 0;
  $data['file_results'] = $this->files_model->select_file_by_id($file_id);

  if($this->input->post('update_file')){
   $this->form_validation->set_error_delimiters('<div class="validation_errors">×', '</div>');
    $this->form_validation->set_rules('case_nr', 'Case Nr.', 'trim|required|max_length[50]');
    $this->form_validation->set_rules('subject', 'Subject', 'trim|required|max_length[50]');
    $this->form_validation->set_rules('content', 'Content', 'trim|required');
    $this->form_validation->set_rules('description', 'Description', 'trim|max_length[1000]');
    $this->form_validation->set_rules('priority', 'Priority', 'trim|required');
    
    if ($this->form_validation->run() == TRUE) {
      
     $result = $this->files_model->update_file($file_id);

     if ($result == TRUE) {
      
      $this->session->set_flashdata('file_upd_succ', 'File has been updated successfully');
      redirect('home');
     }
     else{

      $this->session->set_flashdata('file_upd_fail', 'File failed to be updated');
     }
    }
    else{

     $this->form_validation->set_error_delimiters('<div class="validation_errors">×', '</div>');
     $this->session->set_flashdata('validation_errors', validation_errors());
    }
   }

  $this->load->view('includes/template', $data);
}
#5

[eluser]Aken[/eluser]
You're missing a really important part of validation -- checking the file ID. You should add some sort of error check after you select_file_by_id to see if it returned a valid file object. Because if it doesn't, you obviously don't want to let the user edit it.

The error you're getting is self-explanatory -- you're using a variable like an object, but it isn't an object. Track where the variable was created and figure out why.
#6

[eluser]haris244808[/eluser]
[quote author="Aken" date="1364512294"]You're missing a really important part of validation -- checking the file ID. You should add some sort of error check after you select_file_by_id to see if it returned a valid file object. Because if it doesn't, you obviously don't want to let the user edit it.

The error you're getting is self-explanatory -- you're using a variable like an object, but it isn't an object. Track where the variable was created and figure out why.[/quote]

actually that is checked in the select_file_by_id() method in model.... if the num_row() == 1 it returns TRUE otherwise FALSE....
#7

[eluser]TheFuzzy0ne[/eluser]
So what happens when FALSE is returned by the model?
#8

[eluser]haris244808[/eluser]
[quote author="TheFuzzy0ne" date="1364543828"]So what happens when FALSE is returned by the model?[/quote]

aha ok i catch that...thnx




Theme © iAndrew 2016 - Forum software by © MyBB