Welcome Guest, Not a member yet? Register   Sign In
How to pass a param when using a model method as callable using form validation lib?

(This post was last modified: 10-08-2017, 02:36 PM by happyape.)

This code

           'username', 'Username',
               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

<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

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

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

it seems to print correct output

array(3) {
  string(41) "username_check_duplicate[john.smith]"
  string(32) "username_check_duplicate"
  string(7) "john.smith"

Not sure how to resolve this issue?

Hello happyape,

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


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?

Thanks, I'm on 3.1.7...

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

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

$validation_config = 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) {

(This post was last modified: 02-01-2018, 09:30 AM by Zeff.)

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

and have it validated at once with:

PHP Code:

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-699...#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:

Yes that works well for me.

Theme © iAndrew 2016 - Forum software by © MyBB