我已经创建了一个辅助函数来调用每个文件的函数。基本上该函数正在检查上传的文件是否为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');
据我所知,您可以在同一个类的表单验证中使用回调。因此,将辅助函数定义为Bike_insurance_brochure
类中的方法。之后,您可以使用回调。
但是如果想要创建一个库,还有另一种方法可以做到这一点。您可以扩展本机CI_Form_validation
库并在那里定义方法。
详情CodeIgniter: custom validation rules can't be in a helper?
也尝试没有isset()
功能
if($product_brochure && $product_brochure !=""){
//....
对于上传,你不会让form_validation
工作,因为它只适用于常规表单字段(通过$this->input->post("field_name")
可访问的任何内容)。有关通过表单上传的文件(通过$this->upload->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为您自动执行的许多事情之一)
试试这个例子......它只是一个快速类型,你可以改进它:(在我的例子中,文件字段被称为'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>";
尝试这样做你会让它工作(并且不需要使用辅助函数,因为内置的CI功能已经为你处理几乎所有事情)