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;
    }
}
urfusion
  • 5,528
  • 5
  • 50
  • 87
Shivam Gupta
  • 231
  • 1
  • 2
  • 7

6 Answers6

17

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');
Saty
  • 22,443
  • 7
  • 33
  • 51
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..
Shivam Gupta
  • 231
  • 1
  • 2
  • 7
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

Jayesh Thanki
  • 2,037
  • 2
  • 23
  • 32
  • Code only answers arent encouraged as they dont provide much information for future readers please provide some explanation to what you have written – WhatsThePoint Feb 06 '18 at 10:31
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.

Jomar Gregorio
  • 171
  • 1
  • 3
  • 11
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');

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