Welcome Guest, Not a member yet? Register   Sign In
Form Validation - returns "(Anonymous function)"
#1

(This post was last modified: 03-13-2018, 07:02 AM by sintakonte.)

Hey guys,

i migh've found some bug.

In the docs there is a section called Callable use anything as a rule

I've an object where i do the following
PHP Code:
$this->ci()->load->model('admin/person/Cppersonsave_model');
$this->arrValidationRules[] = array(
    
'field' => 'personid',
    
'label' => 'Duplicatecheck',
    
'rules' => array('check_duplicates', array($this->ci()->Cppersonsave_model,'duplicatesCheck'))
); 




After that i ran the Validation like

PHP Code:
$this->ci()->form_validation->reset_validation();

$this->ci()->form_validation->set_data($arrDbFields);
$this->ci()->form_validation->set_rules($this->arrValidationRules);
if (!
$this->ci()->form_validation->run())
{
    throw new 
FormValidation_Exception(json_encode($this->ci()->form_validation->error_array()));


an excerpt of the model function looks like

PHP Code:
if ($col->count() > 0)
{
    
$this->form_validation->set_message('check_duplicates''This person already exists!');
}

return (
$col->count() > 0   false      true

My model function get called correctly.


But if i do that an error message appears (Anonymous function)

i debugged the code and got the following information 

In line 687 in the Form_validation Library the variable callable is set to true.
On Line 741 the $rule variable gets the callable value which is a boolean and therefore true.


This leads to line 799 which exactly put my message out.

Am i'm missing something here or is this a bug ?

Thx in advance for your answers
Reply
#2

(This post was last modified: 03-13-2018, 07:59 AM by jreklund.)

I haven't tested putting the validation inside a modal. Only in the same libraries. Maybe it can't find the form_validation if it's differs from the file initializing it. But the documentation clearly states that it should work.
I'm doing it like this, querying the modal instead. 

PHP Code:
public function login()
{
 
   $this->load->helper('form');
 
   $this->load->library('form_validation');
 
   
    $validation_rules 
= array(
 
       array(
 
           'field' => 'username',
 
           'label' => lang('login_email'),
 
           'rules' => array(
 
               'trim',
 
               'valid_email'
 
           )
 
       ),
 
       array(
 
           'field' => 'password',
 
           'label' => lang('login_password'),
 
           'rules' => array(
 
               'trim',
 
               'required',
 
               array('validate_auth', array( $this'_validate_auth' ) )
 
           )
 
       )
 
   );
 
   
    $this
->form_validation->set_rules$validation_rules );
 
   
    if
$this->form_validation->run() === TRUE )
 
   {
 
       return TRUE;
 
   }
 
   return FALSE;
}

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

public function _validate_auth()
{
 
   $user_email        $this->input->post('username');
 
   $user_password    $this->input->post('password');
 
   
    if
( empty($user_email) OR empty($user_password) )
 
   {
 
       $this->form_validation->set_message('validate_auth'lang('error_missing_fields'));                
        return FALSE
;
 
   }
 
   
    if
$auth_data $this->auth_model->get_auth_data$user_email ) )
 
   {
 
       if$auth_data->banned === '1' )
 
       {
 
           $this->form_validation->set_message('validate_auth'lang('error_username_password'));
 
           return FALSE;
 
       }
 
       if( ! $this->check_password$auth_data->passwd$user_password ) )
 
       {
 
           $this->form_validation->set_message('validate_auth'lang('error_username_password'));
 
       }
 
       else
        
{
 
           // Setup redirection if redirect required
 
           $this->redirect_after_login();
 
                   
            
// Set session cookie and remember me
 
           $this->maintain_state$auth_data );
 
           
            
// Send the auth data back to the controller
 
           return TRUE;
 
       }
 
   }
 
   else
    
{
 
       $this->form_validation->set_message('validate_auth'lang('error_username_password'));
 
   }
 
   
    return FALSE
;


Maybe you need to use CI() on this too:
PHP Code:
$this->form_validation->set_message('check_duplicates''This person already exists!');
$this->ci()->form_validation->set_message('check_duplicates''This person already exists!'); 
Reply
#3

The callable function needs to be outside any class; it isn't a method.
You haven't shown yours.
Reply
#4

(This post was last modified: 03-14-2018, 01:44 AM by sintakonte.)

i dont understand what you mean @ciadmin 

but nevertheless i solved the problem and it whas on my side

i needed to wrap the rule to an additional array
after that it worked fine (i just thought i don't need it because i do only have one rule)

so the solution was

PHP Code:
$this->arrValidationRules[] = array(
    
'field' => 'personid',
    
'label' => 'Duplicatecheck',
    
'rules' => array(array('check_duplicates', array($this->ci()->Cppersonsave_model,'duplicatesCheck')))
); 
But on the other hand the line 687 of the form_validation Library doesn't make any sense to me
Reply




Theme © iAndrew 2016 - Forum software by © MyBB