由于某些原因,我的分层模型-视图-控制器中的CodeIgniter回调不起作用,但我确实有MY_form_validation库函数。
如果需要或缺失,它应该抛出$error。
我重命名了我的图像文件夹,没有出现错误。你知道我如何通过回调验证让它工作吗?下面是我的代码:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Step_2 extends Controller { // Controller Is renamed In My_Controller.php
public $error = array();
public function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->helper('file');
$this->load->helper('form');
$this->load->library('form_validation');
$this->lang->load('english', 'english');
}
public function index() {
$this->form_validation->set_rules(phpversion(), 'required|callback_validate');
$this->form_validation->set_rules(ini_get('register_globals'), 'required|callback_validate');
$this->form_validation->set_rules(ini_get('magic_quotes_gpc'), 'required|callback_validate');
$this->form_validation->set_rules(ini_get('file_uploads'), 'required|callback_validate');
$this->form_validation->set_rules(ini_get('session_auto_start'), 'required|callback_validate');
$this->form_validation->set_rules(extension_loaded('mysql'), 'required|callback_validate');
$this->form_validation->set_rules(extension_loaded('mysqli'), 'required|callback_validate');
$this->form_validation->set_rules(extension_loaded('pdo'), 'required|callback_validate');
$this->form_validation->set_rules(extension_loaded('pgsql'), 'required|callback_validate');
$this->form_validation->set_rules(extension_loaded('gd'), 'required|callback_validate');
$this->form_validation->set_rules(extension_loaded('curl'), 'required|callback_validate');
$this->form_validation->set_rules(function_exists('mcrypt_encrypt'), 'required|callback_validate');
$this->form_validation->set_rules(extension_loaded('mbstring'), 'required|callback_validate');
$this->form_validation->set_rules(dirname(FCPATH) . '/admin/config/config.php', 'required|callback_validate');
$this->form_validation->set_rules(dirname(FCPATH) . '/catalog/config/config.php', 'required|callback_validate');
$this->form_validation->set_rules(dirname(FCPATH) . '/admin/cache', 'required|callback_validate');
$this->form_validation->set_rules(dirname(FCPATH) . '/catalog/cache', 'required|callback_validate');
$this->form_validation->set_rules(dirname(FCPATH) . '/admin/logs', 'required|callback_validate');
$this->form_validation->set_rules(dirname(FCPATH). '/catalog/logs', 'required|callback_validate');
$this->form_validation->set_rules(dirname(FCPATH) . '/image', 'required|callback_validate');
$this->form_validation->set_rules(dirname(FCPATH) . '/image/cache', 'required|callback_validate');
$this->form_validation->set_rules(dirname(FCPATH) . '/image/catalog', 'required|callback_validate');
$this->form_validation->set_rules(dirname(FCPATH) . '/download', 'required|callback_validate');
if($this->form_validation->run($this)) {
redirect('step_3');
} else {
if (array_key_exists('warning', $this->error)) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
$data['action'] = site_url('step_2');
$data['php_version'] = phpversion();
$data['register_globals'] = ini_get('register_globals');
$data['magic_quotes_gpc'] = ini_get('magic_quotes_gpc');
$data['file_uploads'] = ini_get('file_uploads');
$data['session_auto_start'] = ini_get('session_auto_start');
$data['mysql'] = extension_loaded('mysql');
$data['mysqli'] = extension_loaded('mysqli');
$data['mpdo'] = extension_loaded('pdo');
$data['pgsql'] = extension_loaded('pgsql');
$data['gd'] = extension_loaded('gd');
$data['curl'] = extension_loaded('curl');
$data['mcrypt_encrypt'] = function_exists('mcrypt_encrypt');
$data['zlib'] = extension_loaded('zlib');
$data['iconv'] = function_exists('iconv');
$data['mbstring'] = extension_loaded('mbstring');
$data['config_catalog'] = dirname(FCPATH) . '/catalog/config/config.php';
$data['config_admin'] = dirname(FCPATH) . '/admin/config/config.php';
$data['admin_cache'] = dirname(FCPATH) . '/admin/cache';
$data['catalog_cache'] = dirname(FCPATH) . '/catalog/cache';
$data['admin_logs'] = dirname(FCPATH) . '/admin/logs';
$data['catalog_logs'] = dirname(FCPATH) . '/catalog/logs';
$data['download'] = dirname(FCPATH) . '/download';
$data['image'] = dirname(FCPATH) . '/image';
$data['image_cache'] = dirname(FCPATH) . '/image/cache';
$data['image_data'] = dirname(FCPATH) . '/image/catalog';
$data['back'] = site_url("step_1");
$this->load->view('template/step_2', $data);
}
}
public function validate() {
if (phpversion() < '5.3') {
$this->error['warning'] = 'Warning: You need to use PHP5.3 or above for CI Project to work!';
}
if (!ini_get('file_uploads')) {
$this->error['warning'] = 'Warning: file_uploads needs to be enabled!';
}
if (ini_get('session.auto_start')) {
$this->error['warning'] = 'Warning: CI Project will not work with session.auto_start enabled!';
}
if (!array_filter(array('mysqli', 'pdo', 'pgsql'), 'extension_loaded')) {
$this->error['warning'] = 'Warning: A database extension needs to be loaded in th php.ini for CI Project to work!';
}
if (!extension_loaded('gd')) {
$this->error['warning'] = 'Warning: GD extension needs to be loaded for CI Project to work!';
}
if (!extension_loaded('curl')) {
$this->error['warning'] = 'Warning: CURL extension needs to be loaded for CI Project to work!';
}
if (!function_exists('mcrypt_encrypt')) {
$this->error['warning'] = 'Warning: mCrypt extension needs to be loaded for CI Project to work!';
}
if (!extension_loaded('zlib')) {
$this->error['warning'] = 'Warning: ZLIB extension needs to be loaded for CI Project to work!';
}
if (!function_exists('iconv')) {
if (!extension_loaded('mbstring')) {
$this->error['warning'] = 'Warning: mbstring extension needs to be loaded for CI Project to work!';
}
}
if (!file_exists(dirname(FCPATH) . '/admin/config/config.php')) {
$this->error['warning'] = 'Warning: admin/config.php does not exist. You need to rename admin/config-dist.php to admin/config.php!';
} elseif (!is_writable(dirname(FCPATH) . '/admin/config/config.php')) {
$this->error['warning'] = 'Warning: admin/config.php needs to be writable for CI Project to be installed!';
}
if (!file_exists(dirname(FCPATH) . '/catalog/config/config.php')) {
$this->error['warning'] = 'Warning: config.php does not exist. You need to rename config-dist.php to config.php!';
} elseif (!is_writable(dirname(FCPATH) . '/catalog/config/config.php')) {
$this->error['warning'] = 'Warning: config.php needs to be writable for CI Project to be installed!';
}
if (!is_writable(dirname(FCPATH) . '/admin/cache')) {
$this->error['warning'] = 'Warning: Cache directory needs to be writable for CI Project to work!';
}
if (!is_writable(dirname(FCPATH) . '/catalog/cache')) {
$this->error['warning'] = 'Warning: Cache directory needs to be writable for CI Project to work!';
}
if (!is_writable(dirname(FCPATH) . '/admin/logs')) {
$this->error['warning'] = 'Warning: Logs directory needs to be writable for CI Project to work!';
}
if (!is_writable(dirname(FCPATH). '/catalog/logs')) {
$this->error['warning'] = 'Warning: Logs directory needs to be writable for CI Project to work!';
}
if (!is_writable(dirname(FCPATH) . '/image')) {
$this->error['warning'] = 'Warning: Image directory needs to be writable for CI Project to work!';
}
if (!is_writable(dirname(FCPATH) . '/image/cache')) {
$this->error['warning'] = 'Warning: Image cache directory needs to be writable for CI Project to work!';
}
if (!is_writable(dirname(FCPATH) . '/image/catalog')) {
$this->error['warning'] = 'Warning: Image catalog directory needs to be writable for Riwaka Website Designs CI Project to work!';
}
if (!is_writable(dirname(FCPATH) . '/download')) {
$this->error['warning'] = 'Warning: Download directory needs to be writable for CI Project to work!';
}
if (!$this->error) {
return true;
} else {
return false;
}
}
}发布于 2014-06-26 19:43:37
$this->form_validation->set_rules('input_name_attribute', 'The Name', 'rules|seperated|like|this);http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#validationrules
从用户指南中:
$this->form_validation->set_rules();上面的函数接受三个参数作为输入:
编辑
你应该这样做;
public function index()
{
if ( $this->validate() )
{
echo 'validation passed';
}
else
{
echo 'validation failed!';
}
}表单验证仅适用于表单提交。
https://stackoverflow.com/questions/24429521
复制相似问题