Welcome Guest, Not a member yet? Register   Sign In
Form Validation "ignores" false results of custom functions placed in helper
#11

[eluser]Xelgen[/eluser]
Well, after my previous reply I became curious again, and ran through source of Form_Validation.

I managed to find the root of the problem, but "discovery" really confused me. Smile

Let's start with general stuff.
1) old Validation class (CI 1.6.3) didn't work, and it behaved exactly the way you predicted, in case of bool(false) it still sets function's return as new value.
New class, of CI 1.7.0 checks with is_bool(), and sets a new value to a field, only if function's result is not boolean.

2) now I'm not that much sure that this isn't intended, you'll see why below.

3 ) The core:

In Form_Validation->_execute there is a foreach cycle with some nested if statements, which step-by-step try to determine in which object space the function called in the rule exists, is it callback or not, etc..
After all the check's made, in the end of cycle's code we have validation error check part, dealing with cases when function returns False.

So in case of general php function (no matter native or custom) we'll get by if statements to Line 622, after which function will be finally found.
Then function will be executed, if not boolean, results will be set as field's new value.
And after it, guess what we'll see? continue statement making foreach skip right away to next element and preventing our custom/native functions from being properly checked for false result.

If you make a copy of that "false result case" part of code before continue (just dirty trick to check), everything works exactly as I expected it to work.

As such logical/structural-mistake is highly unbelievable, and it was nearly same in old class, it seems that it was done by intention.
But the reason of it, is still far from being clear to me.

About extending class. Yes, that's what I need, and it will work for me.
But to me (it's of course matter of personal view), extending class, just to place functions, which already do "work by half" without extending original class, feels somewhat wrong.

Again I'm not so much OOP-minded coder.
So the question is:
Let's imagine changes are done, and functions are working the way I was expecting.
Will majority of CI programmers still consider extending class and not placing functions in helper, for this task?
If yes, then I should read more OOP code, and rethink my coding style.
If not, why not to implement such behavior in class, and let programmers organize structure of their programs, in the way they prefer?

Thanks for your replies, they made me go into this all, and find out the reason Smile

P.S. May be it will be better to move this topic to "Bugs Discussion" section of forum?
#12

[eluser]xwero[/eluser]
The use of native php or user defined functions was always in the validation class but it's only meant as a filter for the posted input and not as a rule to check the input with.

The is_bool check it to see if the function returns a success/error or the manipulated string.




Theme © iAndrew 2016 - Forum software by © MyBB