Welcome Guest, Not a member yet? Register   Sign In
Config form_validation file is not reachable in the controller.
#1

[eluser]Unknown[/eluser]
Hi, CI Users!

I got my form_validation.php file placed in the /config directory,
here is the content:

Code:
<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

$config = array(
    'register/index' => array(
        array(
            'field' => 'recaptcha_response_field',
            'label' => 'Captcha',
            'rules' => 'required|callback_check_captcha'
        )
    )
);

?>

According to the CI's manual file, it should be loaded automatically, but it doesnt.
I can't even load it manually using the following code:
Code:
$this->config->load('form_validation');
(same to the autoload).

My register.php controller file:

Code:
<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Register extends CI_Controller {
    var $_rConfig;

    public function index() {
        $this->load->helper( array('form', 'recaptchalib') );
        $this->load->library('form_validation');
        $this->config->load('recaptcha');

        $this->_rConfig = $this->config->item('recaptcha');

        $data['title'] = 'Register';

        $this->load->view('template/header', $data);
        $this->load->view('template/boxes');

        $data = array(
            'header' => 'Register!',
            'login' => 'Login:',
            'password' => 'Password:',
            'rpassword' => 'Repeat Password:',
            'email' => 'Email:',
            'recaptcha' => recaptcha_get_html($this->_rConfig['public']),

            //form
            'submit' => array(
                'value' => 'Submit',
                'class' => 'styled-button'
            )
        );

        $config = array(
            array(
                'field' => 'login',
                'label' => 'Username',
                'rules' => 'required|alpha_dash|min_length[6]|max_length[16]'
            ),
            array(
                'field' => 'password',
                'label' => 'Password',
                'rules' => 'required|min_length[6]|max_length[20]'
            ),
            array(
                'field' => 'rpassword',
                'label' => 'Password Confirmation',
                'rules' => 'required|matches[password]'
            ),
            array(
                'field' => 'email',
                'label' => 'Email',
                'rules' => 'required|valid_email|max_length[100]'
            )
        );

        $this->form_validation->set_rules($config);

    function check_captcha($val) {
            $resp = recaptcha_check_answer(
                    $this->_rConfig['private'],
                    $this->input->ip_address(),
                    $this->input->post('recaptcha_challenge_field', $val),
                    $this->input->post('recaptcha_response_field', $val)
            );

            if ($resp->is_valid)
                return true;
            else {
                $this->form_validation->set_message('recaptcha_response_field', 'Error Ocurred');
                return false;
            }
        }

        if ($this->form_validation->run() == FALSE)
        {
                $this->load->view('register/register', $data);
        }
        else
        {
                $this->load->view('register/register', $data);
        }

        $this->load->view('template/footer');
    }

}

?>

I'm trying to display the errors in the view file then:

Code:
&lt;?= form_error('recaptcha_response_field', '<div class="place"><div class="error">', '</div></div>'); ?&gt;

But no errors appearing, but as you can see I've set field rule to "required".
When I move my form_validation.php file content to the controller, it does work great then, but I need that to stay in the configuration file.

So where is my problem?
#2

[eluser]deqer[/eluser]
I don't think you can do that with config files. = array ( ... ) because you're overriding the originals $config array.

You have to do it like this
$config['register/index'] = array( ... );





Theme © iAndrew 2016 - Forum software by © MyBB