如何从CodeIgniter中的表单验证回调方法返回数据? [英] How to return data from a form validation callback method in CodeIgniter?

查看:64
本文介绍了如何从CodeIgniter中的表单验证回调方法返回数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在CodeIgniter文档中此处

In the CodeIgniter docs here

该部分底部的注释指出:

The note at the bottom of that section states:


您也可以处理传递到回调的表单数据并返回它。如果您的回调函数返回的布尔值是TRUE / FALSE,则假定该数据是您新处理的表单数据。

You can also process the form data that is passed to your callback and return it. If your callback returns anything other than a boolean TRUE/FALSE it is assumed that the data is your newly processed form data.

验证图像上传并执行上传的回调函数,如下所示:

I have a callback function that validates an image upload and does the upload, like this:

public function upload() {
    $this->form_validation->set_rules('imageFile', 'Image', 'callback_validate_image');
    // ...
}

然后是回调函数:

public function validate_image() {

        if (empty($_FILES['imageFile'])) {
            $this->form_validation->set_message('validate_image', 'Please select a file');
            return false;
        }

        $config = array (
            'upload_path' => './uploads/',
            'allowed_types' => 'jpg|jpeg|png|gif',
            'max_size' => '5048576'
        );

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

        if (!$this->upload->do_upload('imageFile')) {
            $this->form_validation->set_message('validate_image', $this->upload->display_errors());
            return false;
        } else {
            $data = array('upload_data' => $this->upload->data());
            return $data;
        }

}

如何返回 $ data upup 函数,以便可以在一个查询中将所有数据插入数据库中?

How can I return the $data to the upload function so that I can insert all data into the database in one query?

推荐答案

如果我做了您的代码部分,我会这样做

If i did your code part, i'll do like this

function upload() 
{

    // 'required' doesn't work on file inputs so use empty()
    if (empty($_FILES['imageFile'])) {
        // show error message
    }

    if ($this->form_validation->run() == FALSE)
    {
            echo "Please select a file";
            //$this->load->view('myform');
    }
    else
    {
        $config = array (
            'upload_path' => './uploads/',
            'allowed_types' => 'jpg|jpeg|png|gif',
            'max_size' => '5048576'
        );

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

        if (!$this->upload->do_upload('imageFile')) 
        {
            $this->form_validation->set_message('validate_image', $this->upload->display_errors());
            return false;
        } 
        else {
            $data['upload_data'] = $this->upload->data();
            $data['form_data'] = $this->input->post(NULL, TRUE);
            var_dump($data);
        }
    }
}




没有经过上传部分。在运行它之前进行测试

Didn't went through the upload part. Test it before run it

这篇关于如何从CodeIgniter中的表单验证回调方法返回数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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