如何在此Codeigniter 3应用程序中将与图像上传相关的验证错误存储为闪光信息? [英] How can I store validation errors related to image upload as flash massages in this Codeigniter 3 application?

查看:70
本文介绍了如何在此Codeigniter 3应用程序中将与图像上传相关的验证错误存储为闪光信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Codeigniter 3.1.8 Bootstrap 4 开发基本的博客应用

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:

$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 here).

对于 update()方法,由于涉及重定向,因此 all 表单验证消息存储为闪存消息。

For the update() method however, since there are redirects involved, all form validation messages are stored as "flash messages".

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());
        // this line was added later
        // but the bug persists 
        $this->session->set_flashdata('upload_errors', $errors);
        redirect('/dashboard/posts/edit/' . $slug);
    }
}

编辑栏中.php 视图我有:

<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>

我无法将图像上传错误消息添加为 Flash消息。

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

我该怎么做?

推荐答案

在edit-post.php中,您必须更改:

In edit-post.php you have to change:

<?php if ($this->session->flashdata('upload_errors')) { ?>

使用:

<?php if ($upload_errors = $this->session->flashdata('upload_errors')) { ?>

这篇关于如何在此Codeigniter 3应用程序中将与图像上传相关的验证错误存储为闪光信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆