-
ardavan
Member
-
Posts: 114
Threads: 32
Joined: Jan 2015
Reputation:
0
07-29-2017, 08:42 AM
(This post was last modified: 07-30-2017, 02:35 AM by ardavan.)
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
- My custom validation rule method returns FALSE even when have only return TRUE; in the method!
- I can't set the error message inside the method, seems I should add in form_validation_lang.php in the /language folder. But all tutorials I've read was setting the message in the method and never mentioned about form_validation_lang.php
for problem #1
config.php
PHP Code: $config['subclass_prefix'] = '';
autoload.php
PHP Code: $autoload['libraries'] = array( 'database', 'email', 'session', 'form_validation', 'pagination', 'encryption');
My custom rule located at application/libraies/AdvanceValidation.php
PHP Code: <?php defined('BASEPATH') OR exit('No direct script access allowed');
class AdvancedValidation extends CI_Form_validation { public function __construct($config = array()) { parent::__construct($config); }
public function scanEmail( $email ) { return TRUE; } }
Use my custom rule in my controller
PHP Code: $this->form_validation->set_rules('email', '', 'scanEmail'); var_dump($this->form_validation->run()); // ALWAYS RETURNS FALSE
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 defined('BASEPATH') OR exit('No direct script access allowed');
class AdvancedValidation extends CI_Form_validation { protected $CI; public function __construct($config = array()) { parent::__construct($config); $this->CI =& get_instance(); }
public function scanEmail( $email ) { if ( empty($email) ) { $this->CI->form_validation->set_message('scanEmail', 'Email address is empty'); return FALSE; } if ( preg_match('/[\'^£$%&*()}{#~?><>,|=+¬]/', $email) ) { $this->CI->form_validation->set_message('scanEmail', 'Do not use special chars'); return FALSE; } reutrn TRUE; } }
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
-
skunkbad
Senior Citizen
-
Posts: 1,300
Threads: 63
Joined: Oct 2014
Reputation:
86
-
ardavan
Member
-
Posts: 114
Threads: 32
Joined: Jan 2015
Reputation:
0
(07-29-2017, 09:07 AM)skunkbad Wrote: You're doing it wrong. Try this:
https://codeigniter.com/user_guide/libra...-as-a-rule
I'm kind of confuse with this user guide
I tried this [doesn't work]
PHP Code: $this->form_validation->set_rules('email', '', array( array($this->AdvancedValidation, 'scanEmail') ));
The Error is
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
ERROR - 2017-07-30 11:19:48 --> Severity: Warning --> method_exists() expects parameter 2 to be string, array given /Applications/XAMPP/xamppfiles/htdocs/xyz/system/libraries/Form_validation.php 749
ERROR - 2017-07-30 11:19:48 --> Severity: Warning --> function_exists() expects parameter 1 to be string, array given /Applications/XAMPP/xamppfiles/htdocs/xyz/system/libraries/Form_validation.php 753
validation_errors()
Code: bool(false) string(101) "
Unable to access an error message corresponding to your field name email.(Anonymous function)
"
However, when I use Anonymous Function it work which I'm not looking for
PHP Code: $this->form_validation->set_rules('email', '', array( function($value) { return TRUE; } ));
-
Martin7483
Crossfire CMS
-
Posts: 373
Threads: 14
Joined: Sep 2015
Reputation:
20
07-30-2017, 02:11 AM
(This post was last modified: 07-30-2017, 02:12 AM by Martin7483.)
Problem 1:
PHP Code: $this->form_validation->set_rules('email', '', 'scanEmail|required'); var_dump($this->form_validation->run()); // ALWAYS RETURNS FALSE
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', array( 'required' => 'Email address is mandatory' 'scanEmail' => 'Do not use special chars' ) );
-
ardavan
Member
-
Posts: 114
Threads: 32
Joined: Jan 2015
Reputation:
0
(07-30-2017, 02:11 AM)Martin7483 Wrote: Problem 1:
PHP Code: $this->form_validation->set_rules('email', '', 'scanEmail|required'); var_dump($this->form_validation->run()); // ALWAYS RETURNS FALSE
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', array( 'required' => 'Email address is mandatory' 'scanEmail' => 'Do not use special chars' ) );
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)?
-
Martin7483
Crossfire CMS
-
Posts: 373
Threads: 14
Joined: Sep 2015
Reputation:
20
07-30-2017, 02:48 AM
(This post was last modified: 07-30-2017, 02:52 AM by Martin7483.)
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)
-
ardavan
Member
-
Posts: 114
Threads: 32
Joined: Jan 2015
Reputation:
0
(07-30-2017, 02:48 AM)Martin7483 Wrote: 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)
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
-
ardavan
Member
-
Posts: 114
Threads: 32
Joined: Jan 2015
Reputation:
0
07-30-2017, 07:10 PM
(This post was last modified: 07-30-2017, 07:11 PM by ardavan.)
(07-30-2017, 04:03 AM)Martin7483 Wrote: 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.
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
Severity: Error
Message: Class 'CI_Form_validation' not found
Filename: libraries/Form_validation.php
Line Number: 5
Backtrace:
|