【问题标题】:custom config file not working with form validation自定义配置文件不适用于表单验证
【发布时间】:2013-01-06 06:32:27
【问题描述】:

我在 application/config/validation_rules.php 中设置了验证规则,它看起来像这样 (短版)

$config = array(
        'member/register' => array(
            'field' => 'language',
            'label' => 'language',
            'rules' => 'required|min_length[5]|max_length[12]'
        ),
        array(
            'field' => 'email',
            'label' => 'email',
            'rules' => 'required|valid_email'
        ),
        array(
            'field' => 'password',
            'label' => 'password',
            'rules' => 'required|min_length[8]'
        ),
        array(
            'field' => 'verify_password',
            'label' => 'password',
            'rules' => 'required|min_length[8]|matches[password]'
        ));

我这样称呼它:

$this->config->load('validation_rules');
$this->form_validation->set_rules($config);
if($this->form_validation->run('member/register') == FALSE)
{
    $page = array(
            'meta_title' => 'member registration',
            'load_page' => 'front/register_view'
    );

    $this->load->view('front/template', $page);
}

validation_errors() 函数不仅没有显示任何内容,而且我也收到此错误:

Message: Undefined variable: config

更新:(这是我的控制器)

class register extends CI_Controller
{
    function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
    }
    function index()
    {
        $this->config->load('validation_rules', TRUE);
        $this->form_validation->set_rules($this->config->item('config', 'validation_rules'));
        if($this->form_validation->run('member/register') == FALSE)
        {
            //validation doesnt pass, load view
            $page = array(
            'meta_title' => 'member registration',
            'load_page' => 'front/register_view'
            );

            $this->load->view('front/template', $page);
        }
        else
        {
            $register_data = array(
            'language' => $this->input->post('language'),
            'email' => $this->input->post('email'),
            'password' => md5($this->input->post('password')),
            'fname' => $this->input->post('fname'),
            'lname' => $this->input->post('lname'),
            'phone' => $this->input->post('phone'),
            'address' => $this->input->post('address'),
            'address2' => $this->input->post('address2'),
            'city' => $this->input->post('city'),
            'state' => $this->input->post('state'),
            'zipcode' => $this->input->post('zipcode'),
            'gfname' => $this->input->post('gfname'),
            'glname' => $this->input->post('glname'),
            'gphone' => $this->input->post('gphone')
            );
            $this->session->set_userdata($register_data);

        }
    }


    function package()
    {
        $page = array(
        'meta_title' => 'Register Package',
        'load_page' => 'register_package_view'
        );
        $this->load->view('includes/template', $page);
    }




}

【问题讨论】:

  • 请看下面,你没有正确地从配置文件中访问变量,所以 $config 永远不存在并导致上述错误。由于 $config 包含规则,因此这些规则从未运行过。

标签: php codeigniter validation


【解决方案1】:

我遇到了同样的问题,但我设法通过使用以下配置解决了它:

在我的应用程序/config/form_validation.php 中:

$config = array(
    "register" => array(
        array(
            "field" => "username",
            "label" => "Username",
            "rules" => "required"
        )
    )
);

在 application/config/autoload.php 中自动加载自定义配置文件“form_validation.php”:

$autoload['config'] = array('form_validation');

在我的控制器中:

// manually set rules by taking $config["register"] from form_validation.php
$this->form_validation->set_rules($this->config->item("register"));

// call run() without parameter
if ($this->form_validation->run() == FALSE) {
    $this->load->view("user/register_test");
} else {
    echo "Form content is correct";
}

我尝试使用$this->form_validation->run("register") 调用验证器,而不使用$this->form_validation->set_rules() 函数,但我没有运气。通过从 form_validation.php 中的配置数组中检索规则来手动设置规则让我很开心。

【讨论】:

    【解决方案2】:

    如果您要扩展 form_validation 库,则需要将 $config 数组传递给父构造函数:

    class MY_Form_validation extends  CI_Form_validation {
    
    /**
     * constuctoooor
     */
    function MY_Form_validation($config){
        parent::__construct($config);
    
    }
    

    http://ellislab.com/forums/viewthread/181937/

    使用文档中概述的方法也更清洁:http://ellislab.com/codeigniter%20/user-guide/libraries/form_validation.html#savingtoconfig 避免调用$this->form_validation->set_rules(...);

    /**
     * This is the POST target for the password reset form above
     * @return null
     */
        public function submit(){
    
            // perform validation //
            if($this->form_validation->run() == FALSE){
                // display error on sign-up page //
                $this->session->set_flashdata("system_validation_errors", validation_errors());
                redirect('member/forgot/password');
            }
    
            // more awesome code
        }
    

    【讨论】:

      【解决方案3】:
      $this->config->load('validation_rules');
      $this->form_validation->set_rules($config);
      

      应该是:

      $this->config->load('validation_rules', TRUE);
      $this->form_validation->set_rules($this->config->item('validation_rules', 'validation_rules'));
      

      根据documentation

      // Loads a config file named blog_settings.php and assigns it to an index named "blog_settings"
      $this->config->load('blog_settings', TRUE);
      
      // Retrieve a config item named site_name contained within the blog_settings array
      $site_name = $this->config->item('site_name', 'blog_settings');
      

      你的规则错了,你忘了把验证组放在一个数组里:

      $config['validation_rules'] = array(
              'member/register' => array(
                  array(
                      'field' => 'language',
                      'label' => 'language',
                      'rules' => 'required|min_length[5]|max_length[12]'
                  ),
                  array(
                      'field' => 'email',
                      'label' => 'email',
                      'rules' => 'required|valid_email'
                  ),
                  array(
                      'field' => 'password',
                      'label' => 'password',
                      'rules' => 'required|min_length[8]'
                  ),
                  array(
                      'field' => 'verify_password',
                      'label' => 'password',
                      'rules' => 'required|min_length[8]|matches[password]'
                  )
              )
      );
      

      【讨论】:

      • 感谢您的评论,但 validation_errors() 函数仍然返回空。我已经用控制器源更新了上面的这篇文章。
      • @SarmenB.,您的验证组不在数组中。每个组都需要像上面那样分组到一个数组中。
      • 谢谢,不知道,由于某种原因它仍然没有显示。我开始放弃了,谢谢你的帮助:)
      • @SarmenB.,不要放弃 =o) 试试我上面帖子底部的代码,这样我们就可以收集一些调试信息。
      • @SarmenB。这样做之后,告诉我你得到的输出,你应该有YYY,一个数组输出,和AAA
      猜你喜欢
      • 1970-01-01
      • 2019-03-08
      • 1970-01-01
      • 2022-11-06
      • 2019-05-20
      • 1970-01-01
      • 2016-08-04
      • 2022-01-21
      • 2023-03-16
      相关资源
      最近更新 更多