CodeIgniter - Unable to access an error message corresponding to your field name Password.(pword_check)
Asked Answered
J

6

9

I am a new to codeIgniter and I just got stuck in the very beginning. I am using HMVC extention and while validating I am getting the following error:

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

any help would be greatly appreciated

Code:

public function submit()
{


    $this->load->library('form_validation');

    $this->form_validation->set_rules('username', 'Username', 'required|max_length[30]|xss_clean');
    $this->form_validation->set_rules('pword', 'Password', 'required|max_length[30]|callback_pword_check|xss_clean');

    if ($this->form_validation->run() == FALSE)
    {
        $this->login();
    }
    else
    {
        echo 'Success'; die();
    }
}

public function pword_check($str)
{

    if ($str == 'test')
    {
        $this->form_validation->set_message('pword_check', 'The %s field can not be the word "test"');
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}
Jacquelinjacqueline answered 19/9, 2015 at 7:47 Comment(2)
Which version of CI you use???Turfman
I am using CodeIgniter Version 3.0.1Jacquelinjacqueline
T
18

xss_clean is no longer part of form validation in Codeingitore 3

Just remove xss_clean from your validation roul

$this->form_validation->set_rules('pword', 'Password', 'required|max_length[30]|callback_pword_check');

If you really, really need to apply that rule, you should now also load the Security Helper, which contains xss_clean() as a regular function and therefore can be also used as a validation rule.

go to application/config/autoload.php :

$autoload['helper'] = array('security');

Or, before your form validation

$this->load->helper('security');
Turfman answered 19/9, 2015 at 8:9 Comment(6)
Thanks for you valuable time. I have already tried removing xss_clean thing also I have auto loaded the security helper but error message is still getting displayed for password field.Jacquelinjacqueline
It's a very simple code I don't know what is causing this error.Jacquelinjacqueline
change your message to $this->form_validation->set_message('pword_check', "The %s field can not be the word test"); and checkTurfman
I changed the message to yours but it did't help, again same error (Unable to access an error message ) is getting displayed.Jacquelinjacqueline
This peice of code looks okk. My be problem is with some other partTurfman
hmmm. but its strange.Jacquelinjacqueline
J
14

Hello Guys I found the solution for working with call_backs in hmvc codeIgniter. I would like to post the solution for others.

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

  1. And change if ($this->form_validation->run() == FALSE) to if ($this->form_validation->run($this) == FALSE) thats all folks..
Jacquelinjacqueline answered 20/9, 2015 at 10:31 Comment(3)
added to libraries in CI 3.0.x, @kev_mSaving
Can we have an explanation as to why this works? Thanks a million by the way, saved my life!Yardage
million thanx, Just a suggestion , you dont need to create the library, instead just pass $this in run function and you are all set $this->form_validation->run($this)Guendolen
W
3
public function pword_check($str)
{

    if ($str == 'test')
    {
        $this->form_validation->set_message(  __FUNCTION__ , 'The %s field can not be the word "test"');
        return FALSE;
    }
    else
    {
        return TRUE;
    }
}

// Its working well

https://github.com/bcit-ci/CodeIgniter/issues/3908

Wheedle answered 6/2, 2018 at 10:9 Comment(1)
Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have writtenSedation
M
1

For future searches. There are two 3 things you need to check when this error shows.

  1. Check the name of the callback function if it is the same with the

    $this->form_validation->set_rules('pword', 'Password', 'required|max_length[30]|callback_pword_check

    public function pword_check($str){                                              
      if ($str == 'test'){
        $this->form_validation->set_message('pword_check', 'The %s field can not be the word "test"');
        return FALSE;
      }
      else{
        return TRUE;
      }                                                                              
    }
    

    *public function pword_check($str){

    *set_message('pword_check', 'The %s field...

  2. In codeigniter 2.X you can do this

    $this->form_validation->run($this) == TRUE

    In 3.x it should be like this

    $this->form_validation->run() == TRUE

    and you need to add this two line of codes on __construct()

    function __construct(){                                     
      parent::__construct();                                                      
      $this->load->library('form_validation');
      $this->form_validation->CI =& $this;                                           
    } 
    
  3. Add this file on your application/libraries/MY_Form_validation.php

    <?php                                                                
    class MY_Form_validation extends CI_Form_validation{                                     
        public $CI;                                                                                               
    } 
    

Cheers! See https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/src/f77a3fc9a6fd?at=codeigniter-3.x

https://www.youtube.com/watch?v=pp4Y_bIhASY&list=PLBEpR3pmwCayNcTCUWlUToK4qIQfFQCCm&index=8

For reference.

Madelene answered 16/6, 2018 at 8:20 Comment(0)
T
0

The reason for this error is you didn't loaded the security helper the following is the way enable security helper with autoload.php in config folder or you can directly load the helper as mentioned last line of this post

And if, despite everything, you really need it, go to application/config/autoload.php :

$autoload['helper'] = array('security'); Or, before your form validation $this->load->helper('security');

Thee answered 31/8, 2016 at 18:6 Comment(0)
M
0

before going to the helper security check on the file application/config/autoload.php add form_validation in librariies

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

and don't forget to add security

$autoload['helper'] = array('security');

try it

Massarelli answered 2/5, 2020 at 4:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.