CodeIgniter Forums
How to pass a param when using a model method as callable using form validation lib? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: How to pass a param when using a model method as callable using form validation lib? (/showthread.php?tid=69101)



How to pass a param when using a model method as callable using form validation lib? - happyape - 10-08-2017

This code

Code:
       $this->form_validation->set_rules(
           'username', 'Username',
           array(
               'required',
               array('username_callable', array($this->usersmodel, 'username_check_duplicate['.$data['username'].']'))
               //array('username_callable', array($this->usersmodel, 'username_check_duplicate'))
           ), array('username_callable' => 'Username duplicate'
           
           ')
       );

Throws this error

Code:
<h4>A PHP Error was encountered</h4>

<p>Severity: Warning</p>
<p>Message:  preg_match() expects parameter 2 to be string, array given</p>
<p>Filename: libraries/Form_validation.php</p>
<p>Line Number: 693</p>

This is line 693 from the Form_validation.php

Code:
if ( ! $callable && preg_match('/(.*?)\[(.*)\]/', $rule, $match))

Is this a bug as I should be able to pass a param using square brackets?

If I remove square brackets i.e.    array('username_callable', array($this->usersmodel, 'username_check_duplicate')) then I don't get this warning but I do need to pass a value.

However, if I test this in a test.php

Code:
    $rule= 'username_check_duplicate['.$data['username'].']';
        preg_match('/(.*?)\[(.*)\]/', $rule, $match);
        var_dump($match);

it seems to print correct output

Code:
array(3) {
  [0]=>
  string(41) "username_check_duplicate[john.smith]"
  [1]=>
  string(32) "username_check_duplicate"
  [2]=>
  string(7) "john.smith"
}

Not sure how to resolve this issue?


RE: How to pass a param when using a model method as callable using form validation lib? - Zeff - 02-01-2018

Hello happyape,

I'm getting the same error. I saw your thread stayed unsolved, were you able to fix it? Please let me know!

Zeff


RE: How to pass a param when using a model method as callable using form validation lib? - happyape - 02-01-2018

Hi Zeff, No I don't think I got that fixed. I just had to use a different approach to resolve that. Which version of CI are you using? Maybe worth upgrading to the latest and see if it has been fixed?


RE: How to pass a param when using a model method as callable using form validation lib? - Zeff - 02-01-2018

Thanks, I'm on 3.1.7...


RE: How to pass a param when using a model method as callable using form validation lib? - happyape - 02-01-2018

I think that's a bug then and needs to be reported?


RE: How to pass a param when using a model method as callable using form validation lib? - happyape - 02-01-2018

Also, instead of using a Callable, just create a rule like this

$validation_config = array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'trim|required|min_length[6]|callback_username_duplicate_check_edit['.$data_selected_user['username'].']'
));

and in your validation method, you can access it like as below ($rule_param_value is the value passed above in the rule)

function username_duplicate_check_edit($new_value, $rule_param_value) {
//
}


RE: How to pass a param when using a model method as callable using form validation lib? - Zeff - 02-01-2018

Problem starts if you create a config or validation array like:
PHP Code:
$validation_rules = array(
 
 array(
 
   'field' => 'some field',
 
   'label' => 'Some Field',
 
   'rules' => array( 'required'
         
   array(
 
                          'date_check'
 
                          array( $this->user_model'checkdatefunction')
 
                   ),
 
              ),
 
 ),
 
 array(
 
   'field' => 'age',
 
   'label' => 'Your age',
    'rules' => 'required|integer',
 
 ),
); 

and have it validated at once with:

PHP Code:
form_validation->set_rules($validation_rules


You get a "Parse error: syntax error, unexpected '$this' (T_VARIABLE), expecting ')' "

If tested it by iterating all fields separately (see thread https://forum.codeigniter.com/thread-69930-post-351098.html#pid351098) and that worked fine!

So it seems if you are setting rules using an array, custom rule validation does not work...

BTW: have you already the test below?
PHP Code:
$this->form_validation->set_rules('username''Username''trim|required|min_length[6]|callback_username_duplicate_check_edit['.$data_selected_user['username'].']'); 



RE: How to pass a param when using a model method as callable using form validation lib? - happyape - 02-01-2018

Yes that works well for me.