![]() |
Use anything as a rule and $this->some_model issue - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11) +--- Thread: Use anything as a rule and $this->some_model issue (/showthread.php?tid=69930) |
Use anything as a rule and $this->some_model issue - Zeff - 01-31-2018 I'm trying to perform form validation from within a specific method ('my_user_model', in fact I'm extending on Lonnie Ezell's MY_Model using 'in model' validation), but I'm getting issues with custom validation rules. Callbacks didn't work because the callback could not be found in 'my_user_model'... So I've gone through the new user guide, where I read about 'use-anything-as-a-rule' in the form_validation user guide. But when I try to validate a field in my controller in this way: PHP Code: $this->form_validation->set_rules('birthdate', 'Birthdate', I'm getting a parse error on '$this' : "syntax error, unexpected '$this' (T_VARIABLE), expecting ')'" Does somebody has experience with this new extension ('Use anything as a rule')? Thanks! RE: Use anything as a rule and $this->some_model issue - Elias - 01-31-2018 You forget last ')' RE: Use anything as a rule and $this->some_model issue - skunkbad - 01-31-2018 Any attempt at formatting/indenting your code would have shown you the problem. PHP Code: $this->form_validation->set_rules( 'birthdate', 'Birthdate', [ RE: Use anything as a rule and $this->some_model issue - Zeff - 02-01-2018 Hi guys, Many thanks for your attentiveness and help, but this thread really helped me out: https://stackoverflow.com/questions/42632186/unable-to-access-an-error-message-corresponding-to-your-field-name-membership-nu I hope this thread can save time to others who struggle with this new option in the CI form_validation library. So, now my (complete) model code looks like this (and works!): PHP Code: <?php defined('BASEPATH') OR exit('No direct script access allowed'); RE: Use anything as a rule and $this->some_model issue - Zeff - 02-02-2018 UPDATE: I tested again an it seems to be working perfectly if you call a validation array too. ![]() The only difference with my live example (which throws 'T_VARIABLE' parse error on $this) is that I assign my validation array (cfr. MY_Model - L. Ezell) as the property $validation_rules at the beginning of my extending child class. PHP Code: // eg. Now I found this thread: 'Can't concatenate on an array' and the proposed solution was that you have to assign such arrays (containing concanations and even methods starting with $this->) in a method... So I changed my code (assigned the array in the constructor) to: PHP Code: Class User_model extends MY_Model And YES: the problem was solved ... Does anyone has an explanation for this? Best regards, Zeff RE: Use anything as a rule and $this->some_model issue - jreklund - 02-02-2018 $this only get's populated/generated after a class have been initiated. And therefor you get an error message when PHP tries to parse your class. It's looking for $this before it exists. RE: Use anything as a rule and $this->some_model issue - Zeff - 02-02-2018 Of course... I was dull searching for an explanation, I missed that one. Many thanks jreklund! RE: Use anything as a rule and $this->some_model issue - b126 - 02-21-2019 How do you proceed if you have to send two parameters to your callable? I am trying since hours and I don't get any solution, since I need to compare two dates (start <= end). In your example, how would you proceed to call checkdateformat(date1,date2)? Thank you RE: Use anything as a rule and $this->some_model issue - InsiteFX - 02-21-2019 Send the two parameters in an array. |