0

I have a problem in my code. I am creating a simple login using CI3 for my small project. My problem is I have an error message in callback validation.

Here's the error I received whenever I try to validate my form.

Unable to access an error message corresponding to your field name Password.(check_database)

Here's mo code in controller:

public function index() {

        $this->form_validation->set_rules('username', 'Username', 'trim|required');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database');

        $this->form_validation->set_error_delimiters('<div class="error text-red">', '</div>');

        if($this->form_validation->run() == FALSE) {

            $data = array();
            $data['modules'] = $this->flx_lib->moduler($data);
            $this->load->view('login', $data);

        } else {


            //ok
        }

    }

    public function check_database($password) {

        $username = $this->input->post($username);
        $result = $this->flx_users->validate_user($username, $password);

        if($result) {
            //ok
            return TRUE;
        } else {
            $this->form_validation->set_message('check_database', 'Invalid username or password');
            return FALSE;
        }

    }

Here's my view:

<div class="form-group has-feedback <?php error_exists('username'); ?>">
                <input type="text" class="form-control" name="username" placeholder="Username" />
                <span class="fa fa-user form-control-feedback "></span>
                <?php echo form_error('username'); ?>
            </div>

            <div class="form-group has-feedback <?php error_exists('password'); ?>">
                <input type="password" class="form-control" name="password" placeholder="Password">
                <span class="glyphicon glyphicon-lock form-control-feedback"></span>
                <?php echo form_error('password'); ?>
            </div>

Can you help me with this? I am using CI3 with HMVC

Jerielle
  • 7,144
  • 29
  • 98
  • 164

2 Answers2

0
public function index() {

    $this->form_validation->set_rules('username', 'Username', 'trim|required');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|callback_check_database', 
        array('check_database' => 'Invalid username or password.')
    );

    $this->form_validation->set_error_delimiters('<div class="error text-red">', '</div>');

    if($this->form_validation->run() == FALSE) {

        $data = array();
        $data['modules'] = $this->flx_lib->moduler($data);
        $this->load->view('login', $data);

    } else {


        //ok
    }

}

public function check_database($password) {

    $username = $this->input->post($username);
    $result = $this->flx_users->validate_user($username, $password);

    if($result) {
        //ok
        return TRUE;
    } else {
        return FALSE;
    }

}

I recoded this one. Something was mixed up. This is from docs:

$this->form_validation->set_rules('field_name', 'Field Label', 'rule1|rule2|rule3',
    array('rule2' => 'Error Message on rule2 for this field_name')
);
Tpojka
  • 6,996
  • 2
  • 29
  • 39
0

Ok I found the error in my code. Actually its an issue with HMVC form validation.

Here's the link for the correct answer: CodeIgniter - Unable to access an error message corresponding to your field name Password.(pword_check)

Solution:

1. Create MY_Form_validation.php file in libraries folder and paste following code in it.

 if (!defined('BASEPATH')) exit('No direct script access allowed');
    class MY_Form_validation extends CI_Form_validation
    {

    function run($module = '', $group = '')
    {
       (is_object($module)) AND $this->CI = &$module;
        return parent::run($group);
    }
}

    And change if ($this->form_validation->run() == FALSE) to if ($this->form_validation->run($this) == FALSE) thats all folks..
Community
  • 1
  • 1
Jerielle
  • 7,144
  • 29
  • 98
  • 164