自定义配置文件无法使用表单验证

时间:2013-01-06 06:32:27

标签: php codeigniter validation

我在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);
    }




}

3 个答案:

答案 0 :(得分:1)

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

在我的application / config / form_validation.php中:

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

自动加载自定义配置文件" form_validation.php"在application / config / autoload.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中从配置数组中检索规则来手动设置规则,这就是我的一天。

答案 1 :(得分:1)

如果要扩展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
    }

答案 2 :(得分:0)

$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]'
            )
        )
);