【问题标题】:Helper function not working properly in CodeIgniter辅助函数在 CodeIgniter 中无法正常工作
【发布时间】:2019-07-20 18:56:11
【问题描述】:

我已经创建了一个帮助函数调用每个文件的函数。基本上,该函数正在检查上传的文件是否为 pdf/xlsx,但该函数没有返回任何结果。我无法弄清楚错误在哪里。

我的控制器

class Bike_insurance_brochure extends CI_Controller {

    function __construct(){
        parent::__construct();

        if(!is_admin_logged_in())
            redirect("/admin","refresh",301);

        $this->load->model('admin/bike_insurance_brochure_m');
    }

    function index(){
        $this->load->admin_template('bike_insurance_brochure_view');
    }

    function save(){
        $config=array(
            array(
                'field'=>'product_b',
                'label'=>'Product Brochure',
                'rules'=>'trim|callback_check_brochure'
            ),
            array(
                'field'=>'product_w',
                'label'=>'Policy Wordings',
                'rules'=>'trim|callback_check_word'
            ),
            array(
                'field'=>'product_c',
                'label'=>'Policy Claim',
                'rules'=>'trim|callback_check_claim'
            ),
            array(
                'field'=>'company',
                'label'=>'company',
                'rules'=>'trim|max_length[255]'
            ),
            array(
                'field'=>'product',
                'label'=>'product',
                'rules'=>'trim|required'
            )
        );

        $this->form_validation->set_rules($config);
        if($this->form_validation->run()!==FALSE){
            print_r(json_encode($this->bike_insurance_brochure_m->save()));
        }
        else{
            $error= "<ul>";
            $error.= validation_errors("<li>", "</li>");
            $error.= "</ul>";
            print_r(json_encode(['status'=>'false','message'=>$error]));
        }
    }

    function get_product_details($bike_insu_bro_id=NULL){
        if($bike_insu_bro_id==NULL || trim($bike_insu_bro_id)==""){
            print_r(json_encode(['status'=>'false','message'=>'Invalid record selected']));
        }

        print_r(json_encode($this->bike_insurance_brochure_m->get_product_details($bike_insu_bro_id)));

    }

    function update_details($bike_insu_bro_id){
        $config=array(
            array(
                'field'=>'product_b',
                'label'=>'Product Brochure',
                'rules'=>'trim|callback_check_brochure'
            ),
            array(
                'field'=>'product_w',
                'label'=>'Policy Wordings',
                'rules'=>'trim|callback_check_word'
            ),
            array(
                'field'=>'product_c',
                'label'=>'Policy Claim',
                'rules'=>'trim|callback_check_claim'
            ),
            array(
                'field'=>'company',
                'label'=>'company',
                'rules'=>'trim|max_length[255]'
            ),
            array(
                'field'=>'product',
                'label'=>'product',
                'rules'=>'trim|required'
            )
        );
        $this->form_validation->set_rules($config);
        if($this->form_validation->run()!==FALSE){
            print_r(json_encode($this->bike_insurance_brochure_m->update_details($bike_insu_bro_id)));
        }
        else{
            $error= "<ul>";
            $error.= validation_errors("<li>", "</li>");
            $error.= "</ul>";
            print_r(json_encode(['status'=>'false','message'=>$error]));
        }
    }

    function delete($bike_insu_bro_id=NULL){
        if($bike_insu_bro_id==NULL || trim($bike_insu_bro_id)==""){
            print_r(json_encode(['status'=>'false','message'=>'Invalid record selected']));
        }
        print_r(json_encode($this->bike_insurance_brochure_m->delete($bike_insu_bro_id)));
    }

    function get_bike_insurance_brochure_list($page=1){
        $this->load->library('pagination');
        $this->load->library('paginationlib');
        try
        {
            $pagingConfig = $this->paginationlib->initPagination($this->bike_insurance_brochure_m->record_count());
            $list = $this->bike_insurance_brochure_m->get_bike_insurance_brochure_list((($page-1) * $pagingConfig['per_page']),$pagingConfig['per_page']);
            $list["pagination"] = $this->pagination->create_links();
            $list['index_start']=(50*($page-1))+1;
            print_r($this->load->view('admin/ajax/bike_insurance_brochure_list',$list,TRUE));
        }
        catch (Exception $err){}
    }
    function get_bike_product_list(){
        print_r(json_encode($this->bike_insurance_brochure_m->get_product_list($this->input->post('company'))));
    }

function check_brochure(){
   check_brochure($this->input->post('product_b'));
  }
function check_word(){
   check_word($this->input->post('product_w'));
  }
function check_claim(){
    check_claim($this->input->post('product_c'));
  }


}

自定义辅助函数

if (!function_exists('check_brochure'))
{
   function check_brochure($product_brochure){
    $CI =& get_instance();

        $allowed_mime_type_arr = array("application/vnd.openxmlformats-officedocument.wordprocessingml.document","application/pdf","application/msword");
        $mime_brochure = get_mime_by_extension($product_brochure);
        if(isset($product_brochure) && $product_brochure !=""){
            if(in_array($mime_brochure, $allowed_mime_type_arr)){
                echo 'hi';
                return true;
            }else{
                $CI->form_validation->set_message('check_brochure', 'Please select only .pdf/.docx/.doc Product Brochure.');
                echo 'ye';
                return false;
            }
        }else{
            $CI->form_validation->set_message('check_brochure', 'Please choose a file to upload Product Brochure.');
             echo 'bye';
            return false;
        }
    }
}

检查时返回再见,但函数未返回 false,我的意思是错误消息未显示。我的 autoload.php:

$autoload['helper'] = array('url', 'file','form','email_settings','comm_func','html','string','globals');

【问题讨论】:

  • 只是一些开箱即用的想法...如果您使用 CI 的上传功能,使用该功能不允许 PDF 或 XLSX 文件以外的任何内容不是更容易吗?

标签: php codeigniter helper


【解决方案1】:

据我所知,您可以在同一类的表单验证中使用回调。因此,将您的辅助函数定义为Bike_insurance_brochure 类中的方法。之后,您可以用作回调。

但是如果想创建一个库,还有另一种方法可以做到。您可以扩展原生 CI_Form_validation 库并在那里定义方法。

详情CodeIgniter: custom validation rules can't be in a helper?

也可以尝试不使用isset() 函数

if($product_brochure && $product_brochure !=""){
 //....

【讨论】:

  • 我想我已经做到了##efine 你的辅助函数作为 Bike_insurance_brochure 类中的一个方法。之后,您可以用作回调。功能 check_brochure(){ check_brochure($this->input->post('product_b')); } function check_word(){ check_word($this->input->post('product_w')); } function check_claim(){ check_claim($this->input->post('product_c')); }
  • @Swagatika 我已经更新了答案,现在检查没有isset
  • 我也检查过没有 isset,但同样的问题我如何解决它,因为我忘记在调用辅助函数时添加 return。
【解决方案2】:

对于上传,您将无法使用 form_validation,因为它仅适用于常规表单字段(可通过 $this-&gt;input-&gt;post("field_name") 访问的任何内容)。对于通过表单上传的文件(您可以通过$this-&gt;upload-&gt;data() 访问),您需要使用上传库的配置。

在 Codeigniter 上,所有多部分表单都以这种方式工作。我通常通过以下方式简化我的工作流程:

$data = array(
    'upload_data'   => $this->upload->data(),
    'form_data'     => $this->input->post(),
);

所以我以 $data['form_data']['field_name']$data['upload_data']['upload_parameter'] 的身份访问所有内容(upload_parameter 是 CI 自动为您执行的众多操作之一)

试试这个例子......它只是一个快速类型,你可以改进它很多: (在我的示例中,文件字段称为“用户文件”)

// define your settings
$config['upload_path'] = wherever/you/need/the/file/to/end/up;
$config['allowed_types'] = 'pdf|xls|xlsx';

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

// do_upload actually uploads the file and checks that the configuration settings you defined are met
if (!$this->upload->do_upload('userfile'))
{
$error = array('error' => $this->upload->display_errors());
print "<pre>".print_r($error,true)."</pre>";

尝试这样做,您会得到它的工作(并且不需要使用辅助函数,因为内置的 CI 功能已经为您处理了几乎所有事情)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-14
    • 1970-01-01
    • 2016-03-27
    • 2019-04-29
    • 1970-01-01
    • 2020-08-10
    • 2014-01-23
    • 1970-01-01
    相关资源
    最近更新 更多