Welcome Guest, Not a member yet? Register   Sign In
How can I store validation errors related to image upload as flash massages?
#1

(This post was last modified: 02-08-2020, 01:51 AM by Ajax30.)

I am working on a basic blog application with Codeigniter 3.1.8 and Bootstrap 4.

The posts, of course, have main images. There is a default image if no image is uploaded by the user.


There are restrictions on image files a user can upload:



Code:
$config['upload_path'] = './assets/img/posts';
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = '2048';

I have managed to display the upload error messages that correspond to the configuration above (for your curiosity and/or use, the code is [b]here[/b]).
For the update() 
method, however, since there are redirects involved, [i]all[/i] form validation messages are stored as "flash messages".


Code:
public function update() {
    // Form data validation rules
    $this->form_validation->set_rules('title', 'Title', 'required',  array('required' => 'The %s field can not be empty'));
    $this->form_validation->set_rules('desc', 'Short description', 'required',  array('required' => 'The %s field can not be empty'));
    $this->form_validation->set_rules('body', 'Body', 'required',  array('required' => 'The %s field can not be empty'));
    $this->form_validation->set_error_delimiters('<p class="error-message">', '</p>');

    $id = $this->input->post('id');

    // Update slug (from title)
    if ($this->form_validation->run()) {
        $slug = url_title(convert_accented_characters($this->input->post('title')), 'dash', TRUE);
        $slugcount = $this->Posts_model->slug_count($slug, $id);
        if ($slugcount > 0) {
            $slug = $slug."-".$slugcount;
        }
    } else {
        $slug = $this->input->post('slug');
    }

// Upload image
    $config['upload_path'] = './assets/img/posts';
    $config['allowed_types'] = 'jpg|jpeg|png';
    $config['max_size'] = '2048';

    $this->load->library('upload', $config);

    if (isset($_FILES['userfile']['name']) && $_FILES['userfile']['name'] != null) {
        // Use name field in do_upload method
        if (!$this->upload->do_upload('userfile')) {

            $errors = array('error' => $this->upload->display_errors());

            // Dysplay upload validation errors
            // only if a file is uploaded and there are errors
            if (empty($_FILES['userfile']['name'])) {
                $errors = [];
            }

            if (!empty($errors)) {
                $data['upload_errors'] = $errors;
            }

        } else {
            $data = $this->upload->data();
            $post_image = $data[ 'raw_name'].$data[ 'file_ext'];
        }
    }
    else {
        $post_image = $this->input->post('postimage');
    }

    if ($this->form_validation->run() && empty($errors)) {
        $this->Posts_model->update_post($id, $post_image, $slug);
        $this->session->set_flashdata('post_updated', 'Your post has been updated');
        redirect('/' . $slug);
    } else {
        $this->form_validation->run();
        $this->session->set_flashdata('errors', validation_errors());
        redirect('/dashboard/posts/edit/' . $slug);
    }
}


In the edit-post.php view I have:

Code:
<input type="hidden" name="postimage" id="postimage" value="<?php echo $post->post_image; ?>">
<label for="postimage">Upload an image</label>
<div class="form-group">
    <input type="file" name="userfile" id="postimage" size="20">
    <?php if ($this->session->flashdata('upload_errors')) { ?>
        <div class="error-messages">
            <?php if(isset($upload_errors)){
                 foreach ($upload_errors as $upload_error) {
                     echo $upload_error;
                 }
            }?>
        </div>
   <?php } ?>
</div>

I have not been able to add the image upload error messages as "flash messages".



How can I do that?
Reply




Theme © iAndrew 2016 - Forum software by © MyBB