在我的代码点火器项目中,我的回调没有像后的多维数组那样工作
我正在尝试我的回调集消息$key['image']
来显示多维数组post的正确值但当我var转储时,它返回NULL
在我的集合规则中,多维数组验证是有效的。
但由于某种原因,我的回调函数上的$key['image']
set_message('banner_image_regex', 'This banner' .' '. $key['image'] .' '. 'image contains a underscore cannot upload file.')
未拾取。
关于我的回调函数的问题banner_image_regex((我如何让$key['image']
获取post值并显示它。
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Banner_add extends MX_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$data['title'] = "Banner Add";
$this->load->library('form_validation');
$this->form_validation->set_rules('banner_name', 'Banner Name', 'required|callback_validate_form');
$this->form_validation->set_rules('banner_status', 'Banner Status', 'required');
if (!empty($this->input->post('banner_image'))) {
foreach ($this->input->post('banner_image') as $key => $value) {
$this->form_validation->set_rules('banner_image['.$key.'][image]', 'Banner Image', 'callback_banner_image_regex');
}
}
if ($this->form_validation->run($this) == FALSE) {
$this->load->view('template/banner/banner_add', $data);
} else {
redirect('admin/banner/banner_list');
}
}
public function banner_image_regex() {
$banner_image = $this->input->post('banner_image');
foreach ($banner_image as $key => $value) {
if (preg_match('/^[a-z0-9]+$/', $key['image'])) {
return TRUE;
} else {
$this->form_validation->set_message('banner_image_regex', 'This banner' .' '. $key['image'] .' '. 'image contains a underscore cannot upload file.');
return FALSE;
}
}
}
}
鉴于您在表单验证中遵循了有关命名数组字段的CI规范,您已经在向回调函数发送特定的值(字符串/图像名称(。所以它需要像:
public function banner_image_regex($image_name) {
if (preg_match('/^[a-z0-9]+$/', $image_name)) {
return TRUE;
} else {
$this->form_validation->set_message('banner_image_regex', 'This banner' .' '. $image_name .' '. 'image contains a underscore cannot upload file.');
return FALSE;
}
}