How to pass a param when using a model method as callable using form validation lib? |
This code
Code: $this->form_validation->set_rules( Throws this error Code: <h4>A PHP Error was encountered</h4> 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'].']'; it seems to print correct output Code: array(3) { 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! Zeff
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?
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) { // }
Problem starts if you create a config or validation array like:
PHP Code: $validation_rules = array( 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-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: $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[6]|callback_username_duplicate_check_edit['.$data_selected_user['username'].']'); |
Welcome Guest, Not a member yet? Register Sign In |