i am trying to show image upload error message using flashdata
but the code which i shown below is not working fine. What could be the reason?
Please suggest me a solution to solve this issue.
mycontrollerPage.php
class Booksetups extends CI_Controller
{
function book($book_id = 0)
{
$config = array();
$config['base_url'] = 'http://localhost/thexxr.com/Booksetups/book/pgn/';
$config["total_rows"] = $this->Booksmodel->record_count();
$config['uri_segment'] = 4;
$config['per_page'] = 5;
$config['full_tag_open'] = '<div id="pagination">';
$config['full_tag_close'] = '</div>';
//------------not wroking file upload error validation------------------------------------------
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1920';
$config['max_height'] = '1280';
$config['remove_spaces'] = 'TRUE';
$config['file_path'] = 'TRUE';
$config['full_path'] = '/uploads/';
$this->load->library('upload', $config);
$this->upload->do_upload("img1");
$this->upload->do_upload("img2");
//------------------------------------------------------------------------
$this->pagination->initialize($config);
$page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0;
$this->form_validation->set_error_delimiters('<div class="error">', '</div>')->set_rules('subject_id', 'subject_id','trim|required|min_length[1]|max_length[150]|xss_clean');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>')->set_rules('cover_id', 'cover_id','trim|required|min_length[1]|max_length[150]|xss_clean');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>')->set_rules('language_id', 'language_id','trim|required|min_length[1]|max_length[150]|xss_clean');
$this->form_validation->set_error_delimiters('<div class="error">', '</div>')->set_rules('edition_id', 'edition_id','trim|required|min_length[1]|max_length[150]|xss_clean');
if(($this->input->post('book_id'))&& ($this->form_validation->run() === TRUE))
{
if( ! $this->upload->do_upload()) //trying to display error
{
$error = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('error', $error);
redirect(current_url());
}
$this->session->set_flashdata('msg', '1 row(s) affected.');
$this->Booksmodel->entry_update();
redirect(current_url());
}
}
}
Myview.php
<?php echo '<fieldset><legend>'. $formtopic.'</legend>' ?>
<?php echo validation_errors(); ?>
<?php
$message = $this->session->flashdata('msg');
if($message)
{
?>
<div class="success" id="msgdiv"><?php echo $this->session->flashdata('msg'); ?></div>
<?php
}
?>
<?php
$message = $this->session->flashdata('error');
if($message)
{
?>
<?php echo $error;?>
<!-- Here i am getting. Message like "array" why not i am getting the error message instead? -->
<?php
}
?>
I will recommend using form_validation's validation_errors(). Here is how i do it.
Take a look at the library in this answer.
This library has two methods validate_upload (it only checks if file is valid)
and do_upload(must be used only when validate_upload returns true).
There is a file upload library available in Codeigniter Here is the documentation. Copy the code of my answer and paste it in a file called MY_Upload.php and save this file in application/Code folder. And now i define a rule like this
When the image upload is optional you can wrap it in a condition
Where userfile is file field of form. You can see i have called a function in rule callback_valid_upload
Here is method of controller used in callback
This method will check if file we are uploading is valid and will return true on success else false.
When validation is failed load form
And if you want to display the messages seperatly
And if validation is successfull upload file now like this.
MORE EDITS :
One thing i noticed in your code is a problem
Here are some of my helper functions to set and show errors in CI
You can set message from controller :
and show error from view
As I have said in my comment;
If you look at the function in the source code, it either takes a hash array OR a string.
If an array is passed then it will set the flashdata with the key as the flash message type, message with the value; ignoring the second parameter.
If you pass two arguments as strings it will set the flashdata key with the first string.
So either do;
Or
Code from the source: