简体   繁体   中英

Helper function not working properly in CodeIgniter

I have created a helper function call the function each file. Basically the function is checking if the uploaded file is pdf/xlsx, but the function not returning anything as result. I could not figure it out where is the error.

My controller

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'));
  }


}

custom helper function

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

It is returning bye while checking but function is not return to false, I mean error message not showing. My autoload.php:

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

As I know you can use callback in form validation from same class. So define your helper function as a method in Bike_insurance_brochure class. After that you can use as callback.

But If want to make a library there are a another way to do it. You can extend native CI_Form_validation library and define method there.

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

Also try without isset() function

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

For uploads, you won't get the form_validation to work, as it'll only work for regular form fields (anything accessible through $this->input->post("field_name") ). For anything regarding the files uploaded through the form, which you'd access through $this->upload->data() , you need to work with the configuration of the upload library.

On Codeigniter, all multipart forms work this way. I usually simplify my workflow by doing:

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

So I access everything as $data['form_data']['field_name'] and $data['upload_data']['upload_parameter'] ( upload_parameter being one of many things CI does automagically for you)

Try this example... it's just a quick type, you may improve it a lot: (in my example, the file field is called 'userfile')

// 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>";

try doing this and you'll get it to work (and not need the use of a helper function, as the built-in CI functionality already handles almost everything for you)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM