自定义配置文件无法进行表单验证


custom config file not working with form 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);
    }


}

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

在我的应用程序/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中的配置数组中检索规则来手动设置规则,让我的日子过得很愉快。

如果要扩展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/librarys/form_validation.html#保存配置以避免调用$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
    }
$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'));

根据文件:

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