custom config file not working with form validatio

2019-05-19 16:39发布

问题:

i've set the validation rules in application/config/validation_rules.php and it looks like this (short version)

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

and i'm calling it like this:

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

not only is the validation_errors() function not showing anything but i'm also getting this error:

Message: Undefined variable: config

update: (here is my controller)

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




}

回答1:

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

should be:

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

Per the 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');

Your rules are wrong, you forgot to put the validation group in an array:

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


回答2:

I encountered same problem but I managed to fix it by using following configuration:

In my application/config/form_validation.php:

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

Auto-load the custom config file "form_validation.php" inside application/config/autoload.php:

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

In my controller:

// 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";
}

I've tried calling the validator using $this->form_validation->run("register"), without using $this->form_validation->set_rules() function, but I got no luck. Setting the rules manually by retrieving it from config array in form_validation.php make my day.



回答3:

In case you are extending the form_validation library, you need to pass the $config array to the parent constructor:

class MY_Form_validation extends  CI_Form_validation {

/**
 * constuctoooor
 */
function MY_Form_validation($config){
    parent::__construct($config);

}

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

It's also cleaner using the method outlined in the docs: http://ellislab.com/codeigniter%20/user-guide/libraries/form_validation.html#savingtoconfig to avoid calling $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
    }