我已经创建了一个帮助器函数,该函数调用每个文件。基本上,该功能正在检查上传的文件是否为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;
}
}
}
正在检查时重试,但功能不会恢复为假,这意味着未显示错误消息。 我的autoload.php
$autoload['helper'] = array('url', 'file','form','email_settings','comm_func','html','string','globals');
答案 0 :(得分:0)
据我所知,您可以在同一类的表单验证中使用回调。因此,将您的辅助函数定义为Bike_insurance_brochure
类中的方法。之后,您可以用作回调。
但是,如果要创建一个库,还有另一种方法。您可以扩展本机CI_Form_validation
库并在其中定义方法。
详细信息CodeIgniter: custom validation rules can't be in a helper?
也可以尝试不使用isset()
函数
if($product_brochure && $product_brochure !=""){
//....
答案 1 :(得分:0)
对于上载,您将无法使用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功能已经可以为您处理几乎所有的事情)