设置规则不工作CI

时间:2014-06-26 11:37:41

标签: php codeigniter

出于某种原因,我的分层模型 - 视图 - 控制器中的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;
            }
      }
}

1 个答案:

答案 0 :(得分:0)

$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();

上述函数将三个参数作为输入:

  1. 字段名称 - 您给出表单字段的确切名称。

  2. A&#34;人类&#34;此字段的名称,将插入错误消息中。例如,如果您的字段命名为&#34; user&#34;你可以给它一个人名#34;用户名&#34;。注意:如果您希望将字段名称存储在语言文件中,请参阅翻译字段名称。

  3. 此表单字段的验证规则。

  4. 修改

    你应该这样做;

    public function index()
    {
        if ( $this->validate() )
        {
            echo 'validation passed';
        }
        else
        {
            echo 'validation failed!';
        }
    }
    

    您的验证功能正在检查您是否启用了php扩展并加载了库,因此无需使用form_validation。表单验证仅适用于表单提交。