a form validation issue |
Hi,
I have a form validation issue. here's the code Code: $this->form_validation->set_rules('pass','password','required|min_length[8]|callback_access'); When I used CI 3.0.6 , it could run "required", "min_length[8]" and "callback_access" function together. But it won't work together when I update to CI 3.1.0. If I remove "callback_access" function (as bellow), then "required" and "min_length[8]" can be work correctly. Code: $this->form_validation->set_rules('pass','password','required|min_length[8]'); Code: public function access(){ Thanks!!
It sounds like there is something wrong with your callback. Can you post your callback function?
I checked the system/libraries/Form_validation.php file(CI 3.1.0), I was wondering is it because of the _prepare_rules function? So, the callback function and rule's order was change.
It says ""Callbacks" are given the highest priority (always called), followed by 'required' (called if callbacks didn't fail), and then every next rule depends on the previous one passing. So, the "callback" function will be run first, then "required" rule and others? Sorry, I'm not a native English speaker, maybe I misunderstand the meaning.
You posted this on Stack Overflow as well, and there they said the same thing I did - Can you post the callback function so we can have a look at it. There is nothing wrong with CI, it sounds like you have a problem with your callback function.
What version of PHP are you running?
Officially dropped any kind of support for PHP 5.2.x and anything under 5.3.7. What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(08-09-2016, 04:06 AM)PaulD Wrote: You posted this on Stack Overflow as well, and there they said the same thing I did - Can you post the callback function so we can have a look at it. There is nothing wrong with CI, it sounds like you have a problem with your callback function. Yes, I posted this on Stack Overflow too, because I'm not sure I posted here successfully. It didn't show the post immediately. So, I asked the same question there. Then I read the help documents, so I understand that I needed permission to post a new thread. And I posted callback function in #1.
So I created a test view for you:
PHP Code: <p>Message: <?php echo $message; ?></p> And a test controller with the following: PHP Code: $this->form_validation->set_error_delimiters('', ''); With this function to mimic your callback (although simplified, and I create values manually) PHP Code: public function access(){ And it behaved and worked prefectly in latest CI3. Input blank: failed required test with correct error message Input short: failed min_length test with correct error message Input wrong: failed callback password test with correct error message Input correct: passed validation with correct error message There is no issue with callbacks per se. Quote:But it won't work together when I update to CI 3.1.0.What errors are you getting, or what is happening that 'doesn't work'. Try replacing your callback function values with hardcoded ones like I did. If it works then, the problem is in your login library Hope that helps, Paul. PS You definitely should NOT be using md5 to store passwords. https://hashkiller.co.uk/md5-decrypter.aspx Why not use native php password hashing. For example: PHP Code: // encrypting http://php.net/manual/en/function.password-hash.php Code: $this->form_validation->set_message('required', "%s required"); Thanks, Paul. It doesn't work together means: I thought the required rule would run first, then min_length rule, then callback function. First, if password input text is empty(didn't type anything), then I press the submit button, I thought it would show 'password required'. But it didn't , it show the callback message "account or password was incorrect". Second, if I type something in the password input text, but the length was under 8, then it would show "The password field must be at least 8 characters in length.". But it show the callback message "account or password was incorrect". In CI 3.0.6 (Error messages worked well.) Input blank: shows the message 'password required'. Input short: shows the message 'The password field must be at least 8 characters in length.' Input wrong: shows the message 'account or password was incorrect'. Input correct: passed. In CI 3.1.0 Input blank: shows the message 'account or password was incorrect'. (ignore required rule) Input short: shows the message 'account or password was incorrect'. (ignore min_length rule) Input wrong: shows the message 'account or password was incorrect'. (correct) Input correct: passed.(correct) Why were they directly jump to the callback function , but ignore required and min_length rules. These are my issue. Should I separate rules and callback function into a different text rule, such as the code bellow? I mean, Couldn't they set together? Code: $this->form_validation->set_message('required', "%s required"); By the way, I'll use password_hash(). Thank you so much.
My apologies, you are absolutely right.
I was wondering about your problem when I discovered my test site was running 3.06 or something like that. I upgraded to 3.1 and ran the same test I posted before, and the callback is being called first. Looking into this now. Best wishes, Paul. |
Welcome Guest, Not a member yet? Register Sign In |