Problems with extending CI_Form_validation - 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: Problems with extending CI_Form_validation (/showthread.php?tid=68572) Pages:
1
2
|
Problems with extending CI_Form_validation - ardavan - 07-29-2017 Hey guys, before anything, I've read few post about creating custom rules via form_validation extending class but still, I have problems here. PHP 5.6 CI 3.1.5 I've got 2 problem
for problem #1 config.php PHP Code: $config['subclass_prefix'] = ''; autoload.php PHP Code: $autoload['libraries'] = array( My custom rule located at application/libraies/AdvanceValidation.php PHP Code: <?php Use my custom rule in my controller PHP Code: $this->form_validation->set_rules('email', '', 'scanEmail'); How to fix the FALSE return? for problem #2 how can I set the error messages inside of my custom rule methods instead of creating in Form_validation_lang.php? I like to have like this: PHP Code: <?php But always I get an error which is saying Unable to access to the error message of scanEmail. How can I set messages this way without setting in Form_validation_lang.php file? Thanks RE: Problems with extending CI_Form_validation - skunkbad - 07-29-2017 You're doing it wrong. Try this: https://codeigniter.com/user_guide/libraries/form_validation.html#callable-use-anything-as-a-rule RE: Problems with extending CI_Form_validation - ardavan - 07-29-2017 (07-29-2017, 09:07 AM)skunkbad Wrote: You're doing it wrong. Try this: I'm kind of confuse with this user guide I tried this [doesn't work] PHP Code: $this->form_validation->set_rules('email', '', array( Log Code: ERROR - 2017-07-30 11:19:48 --> Severity: Warning --> preg_match() expects parameter 2 to be string, array given /Applications/XAMPP/xamppfiles/htdocs/xyz/system/libraries/Form_validation.php 693 Code: bool(false) string(101) " However, when I use Anonymous Function it work which I'm not looking for PHP Code: $this->form_validation->set_rules('email', '', array( RE: Problems with extending CI_Form_validation - Martin7483 - 07-30-2017 Problem 1: PHP Code: $this->form_validation->set_rules('email', '', 'scanEmail|required'); You have set two rules, but if you post an empty form then Rule 1, scanEmail, will return TRUE Rule 2, required, will return FALSE And that causes $this->form_validation->run() to return FALSE Don't only var_dump() the run method. Place a var_dump() in each rule method that should run and var_dump the passed in arguments. That way you can see if it even reaches a rule or not. Problem 2: You should set the messages when setting the rules. I think because you are doing it only when the rule fails it can't find the message. PHP Code: $this->form_validation->set_rules('email', 'Field Label', 'scanEmail|required', RE: Problems with extending CI_Form_validation - ardavan - 07-30-2017 (07-30-2017, 02:11 AM)Martin7483 Wrote: Problem 1: I've tested without required rule as well. The var_dump never hit when it is in my method in the AdvancedValidation class. This is the main problem. and always it's FALSE. I tried skunkbad's solution but seems doesn't work! Do you have any idea why i cant hit the method in my class? is anything wrong in my setup(look at my first post, pls)? RE: Problems with extending CI_Form_validation - Martin7483 - 07-30-2017 That I didn't spot this from the start :S It's your class name AdvancedValidation. This is not autoloaded, so your rule is not available. That is why it is returning FALSE. When not available, you won't get a PHP error or warning for a non existing method as the validation class uses a method_exists check on called rules. Also this is not how the Loader class works when loading extended classes. Look at the Loader class for more details. If you don't want to use a Class prefix then the class name should be Form_Validation So rename the class (I would do this), or manually load the extended class (which is going to cause more problems than solve them) RE: Problems with extending CI_Form_validation - ardavan - 07-30-2017 (07-30-2017, 02:48 AM)Martin7483 Wrote: That I didn't spot this from the start :S that is the same even when im adding AdvancedValidation into the autoload.php in libraries array. Same result. i didn't get exactly what should i do after reading your solutions. Could you please write some code about it? Thank you for your solutions RE: Problems with extending CI_Form_validation - Martin7483 - 07-30-2017 Rename your class AdvancedValidation to Form_validation Rename the file from AdvancedValidation.php to Form_validation.php When loading CI_Form_validation the extended Form_validation will be auto loaded. RE: Problems with extending CI_Form_validation - ardavan - 07-30-2017 (07-30-2017, 04:03 AM)Martin7483 Wrote: Rename your class AdvancedValidation to Form_validation i followed what you said exactly Form_validation.php PHP Code: class Form_validation extends CI_Form_validation but i got this error. Even if i try this ....extends Form_validation {.... Code: A PHP Error was encountered RE: Problems with extending CI_Form_validation - Martin7483 - 07-31-2017 How are you loading the validation library? |