在Codeigniter中我有add_main_products
函数将图像上传到数据库,在此之前我想验证类似<input type="text" name="product_name">
的文本字段
。我如何在add_main_products()
函数
function add_main_products(){
$config['upload_path'] = 'application/views/uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('admin/admin_add_mainproduct', $error);
} else {
$this->mod_products->add_main_product($this->upload->data());
$data = array('upload_data' => $this->upload->data());
$this->load->view('admin/admin_add_mainproduct', $data); }
}
答案 0 :(得分:3)
试试这个......
function add_main_products()
{
$this->form_validation->set_rules('product_name', $this->lang->line('product_add_name'), 'required|xss_clean');
if ($this->form_validation->run() == TRUE)
{
// Put your upload code
}
}
并在$this->load->library('form_validation');
_construct function
库
答案 1 :(得分:1)
你可以做这样的事情
function add_main_products(){
$this->form_validation->set_rules('product_name', 'Product Name', 'required');
if ($this->form_validation->run() == FALSE){
$this->load->view('admin/admin_add_mainproduct');
}
else{
$config['upload_path'] = 'application/views/uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
if (! $this->upload->do_upload()){
$error['error'] = array('error' => $this->upload->display_errors());
$this->load->view('admin/admin_add_mainproduct', $error);
return FALSE;
}
$this->mod_products->add_main_product($this->upload->data());
$data = array('upload_data' => $this->upload->data());
$this->load->view('admin/admin_add_mainproduct', $data);
}
}