Welcome Guest, Not a member yet? Register   Sign In
Form Validation Callback
#1

(This post was last modified: 04-10-2017, 03:31 AM by RBX.)

I'm using CI 3.1.4, and Validation library can't seem to find my callback function, which goes something like this.

PHP Code:
$this->fv->set_rules('currentPassword''Current Password''required|callback__verifyPassword');

public function 
_verifyPassword($suppliedPassword)
{
 
   $this->fv->set_message(__METHOD__'Incorrect {$field}');
 
   $currentPassword $this->db
        
->select('password')
 
       ->where('id'$this->userData['id'])
 
       ->get(Table::ADMINS)->row()->password;
 
   return $this->AuthModel->passwordVerify($suppliedPassword$currentPassword);


It doesn't matter what I put in the callback function. I always get this:
Quote:Unable to access an error message corresponding to your field name Current Password.(_verifyPassword)

I've tried various names for callback method, but it's of no use. Also, I believe callback function should be called only after the first rule, required fails, but I always see this error message, and nothing about the field being required. If, however, I change callback__verifyPassword to just _verifyPassword, I get error only when I enter something, and error message corresponding to required otherwise.

EDIT:
I get an error message if I define this:
PHP Code:
$lang['_verifyPassword'] = 'Incorrect Password'
but I believe the callback function isn't still called.

EDIT:
I just had a look at CI_Form_validation, and contrary to what I always thought, the validation functions are reordered, and callbacks are always executed.
PHP Code:
    /**
     * Prepare rules
     *
     * Re-orders the provided rules in order of importance, so that
     * they can easily be executed later without weird checks ...
     *
     * "Callbacks" are given the highest priority (always called),
     * followed by 'required' (called if callbacks didn't fail),
     * and then every next rule depends on the previous one passing.
     *
     * @param    array    $rules
     * @return    array
     */ 
Has this changed for previous versions? I can't seem to find anything about this in the documentation. It'd be great if such behavior can be documented.

EDIT:
Sorry for so many edits. I think this is because of the HMVC module I use.
On CI_Form_validation, line 715, it is checked if method exists in $this->CI
PHP Code:
if ( ! method_exists($this->CI$rule)) 
When extending form CI_Controller, this returns true, and when extending from MX_Controller, this returns false.
Reply
#2

(This post was last modified: 04-10-2017, 03:52 AM by InsiteFX.)

Try this:

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

/**
 * ----------------------------------------------------------------------------
 * Editor   : PhpStorm 2017.1.1
 * Date     : 2/5/2017
 * Time     : 6:29 AM
 * Authors  : Raymond L King Sr.
 * ----------------------------------------------------------------------------
 *
 * Class        MY_Form_validation
 *
 * @project     csdmodule
 * @author      Raymond L King Sr.
 * @link        http://www.procoversfx.com
 * @copyright   Copyright (c) 2009 - 2017 Pro Covers FX, LLC.
 * @license     http://www.procoversfx.com/license
 * ----------------------------------------------------------------------------
 */

/**
 * Class MY_Form_validation
 *
 * place into ./application/libraries
 * 
 * use like this
 * if ($this->form_validation->run($this) == FALSE)
 * {
 *
 * }
 * else
 * {
 *
 * }
 */
class MY_Form_validation extends CI_Form_validation
{

    
/**
     * Class properties - public, private, protected and static.
     * ------------------------------------------------------------------------
     */


    // ------------------------------------------------------------------------

    /**
     * run ()
     * ---------------------------------------------------------------------------
     *
     * @param   string $module
     * @param   string $group
     * @return  bool
     */
    
public function run($module ''$group '')
    {
        (
is_object($module)) AND $this->CI = &$module;

        return 
parent::run($group);
    }

    
// ------------------------------------------------------------------------

  // End of MY_Form_validation Class.

/**
 * ----------------------------------------------------------------------------
 * Filename: MY_Form_validation.php
 * Location: ./application/libraries/MY_Form_validation.php
 * ----------------------------------------------------------------------------
 */ 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

According to the documentation your callback should not start with the underscore
see callback
A good decision is based on knowledge and not on numbers. - Plato

Reply
#4

(This post was last modified: 04-10-2017, 09:23 AM by InsiteFX. Edit Reason: spelling error and added message )

An underscore in front of a method name is CI's way of making it private same as using private function name()

But yes according to the documentation a validation callback starts like this callback_

So having an underscore in the method name first would cause it not to run.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

(04-10-2017, 03:51 AM)InsiteFX Wrote: Try this:

That should do it. For now, I've avoided the callback validation rule.

(04-10-2017, 03:53 AM)salain Wrote: According to the documentation your callback should not start with the underscore
see callback
This rule applies to router, and not the PHP itself, therefore it shouldn't cause a problem, and it really doesn't. Also, I can't find mention of what you say on the given link.
Reply
#6

(This post was last modified: 04-10-2017, 11:17 AM by Paradinight.)

(04-10-2017, 11:02 AM)RBX Wrote:
(04-10-2017, 03:51 AM)InsiteFX Wrote: Try this:

That should do it. For now, I've avoided the callback validation rule.

(04-10-2017, 03:53 AM)salain Wrote: According to the documentation your callback should not start with the underscore
see callback
This rule applies to router, and not the PHP itself, therefore it shouldn't cause a problem, and it really doesn't. Also, I can't find mention of what you say on the given link.

$this->fv->set_message(__METHOD__, 'Incorrect {$field}');

change __METHOD__ to _verifyPassword.
make a var_dump to __METHOD__ Tongue
edit:

http://php.net/manual/en/language.consta....php#57033
Reply
#7

@RBX

If you look at the rule and the actual method in the link I am referring as well as into the Form_validation library.

You will see that if you have a callback rule like this:


PHP Code:
$this->form_validation->set_rules('username''Username''callback_username_check'); 


Your method has to be :


PHP Code:
public function username_check($str)
 
    {
 
               ......
 
    

This is because the Form_validation library removes the first 9 characters from the callback method name in the rule, so it includes the underscore. So if you want your method name to start with an underscore you need to double it in the rule:

PHP Code:
$this->form_validation->set_rules('username''Username''callback__username_check');

.....


public function 
_username_check($str)

 
       {
..... 
A good decision is based on knowledge and not on numbers. - Plato

Reply
#8

(04-10-2017, 03:51 AM)InsiteFX Wrote: Try this:

PHP Code:
<?php
defined
('BASEPATH') OR exit('No direct script access allowed');

/**
 * ----------------------------------------------------------------------------
 * Editor   : PhpStorm 2017.1.1
 * Date     : 2/5/2017
 * Time     : 6:29 AM
 * Authors  : Raymond L King Sr.
 * ----------------------------------------------------------------------------
 *
 * Class        MY_Form_validation
 *
 * @project     csdmodule
 * @author      Raymond L King Sr.
 * @link        http://www.procoversfx.com
 * @copyright   Copyright (c) 2009 - 2017 Pro Covers FX, LLC.
 * @license     http://www.procoversfx.com/license
 * ----------------------------------------------------------------------------
 */

/**
 * Class MY_Form_validation
 *
 * place into ./application/libraries
 * 
 * use like this
 * if ($this->form_validation->run($this) == FALSE)
 * {
 *
 * }
 * else
 * {
 *
 * }
 */
class MY_Form_validation extends CI_Form_validation
{

    
/**
     * Class properties - public, private, protected and static.
     * ------------------------------------------------------------------------
     */


    // ------------------------------------------------------------------------

    /**
     * run ()
     * ---------------------------------------------------------------------------
     *
     * @param   string $module
     * @param   string $group
     * @return  bool
     */
    
public function run($module ''$group '')
    {
        (
is_object($module)) AND $this->CI = &$module;

        return 
parent::run($group);
    }

    
// ------------------------------------------------------------------------

  // End of MY_Form_validation Class.

/**
 * ----------------------------------------------------------------------------
 * Filename: MY_Form_validation.php
 * Location: ./application/libraries/MY_Form_validation.php
 * ----------------------------------------------------------------------------
 */ 

(04-10-2017, 11:16 AM)Paradinight Wrote:
(04-10-2017, 11:02 AM)RBX Wrote:
(04-10-2017, 03:51 AM)InsiteFX Wrote: Try this:

That should do it. For now, I've avoided the callback validation rule.

(04-10-2017, 03:53 AM)salain Wrote: According to the documentation your callback should not start with the underscore
see callback
This rule applies to router, and not the PHP itself, therefore it shouldn't cause a problem, and it really doesn't. Also, I can't find mention of what you say on the given link.

$this->fv->set_message(__METHOD__, 'Incorrect {$field}');

change __METHOD__ to _verifyPassword.
make a var_dump to __METHOD__ Tongue
edit:

http://php.net/manual/en/language.consta....php#57033

Yeah, I should have used __FUNCTION__ instead. I can't dump that constant, the method never gets called. Smile

(04-11-2017, 12:01 AM)salain Wrote: @RBX

If you look at the rule and the actual method in the link I am referring as well as into the Form_validation library.

You will see that if you have a callback rule like this:


PHP Code:
$this->form_validation->set_rules('username''Username''callback_username_check'); 


Your method has to be :


PHP Code:
public function username_check($str)
 
    {
 
               ......
 
    

This is because the Form_validation library removes the first 9 characters from the callback method name in the rule,  so it includes the underscore. So if you want your method name to start with an underscore you need to double it in the rule:

PHP Code:
$this->form_validation->set_rules('username''Username''callback__username_check');

.....


public function 
_username_check($str)

 
       {
..... 

Yes, and as can be seen, I'm already using double underscores. It's intentional as validation callbacks need to be public, but I don't want them to be accessible by URL.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB