CodeIgniter Forums
Why use strings when doing rules for Form Validation instead of flags - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Why use strings when doing rules for Form Validation instead of flags (/thread-27368.html)



Why use strings when doing rules for Form Validation instead of flags - El Forum - 02-08-2010

[eluser]ocdcoder[/eluser]
Just out of curiosity, I was playing with the form validation library today and realized that the rules (the parameter separated by "|"s) are doing as a string and not as flags. I could imagine there would be some performance improvement if this was switched to flags, or at least passing in an array instead of a string.
Has anyone else thought of this too?

[Edit]
So after looking at the library more, I see the reasons as to why it shouldn't be flags, although I still think passing an array will be faster. Any reasons why not?


Why use strings when doing rules for Form Validation instead of flags - El Forum - 02-09-2010

[eluser]bretticus[/eluser]
I suppose you pay a tiny bit of processing overhead splitting the strings. However, the strings aren't really that large. If this were something iterative/recursive, I'd be concerned I suppose. I'd probably be looking for performance tweaks in other areas first though if it were me (database, etc.)

You can always extend the Form_validation class to pass in rules to your liking.

On a final note, this post may (or may not) get more mileage under the Ignited Code forum.


Why use strings when doing rules for Form Validation instead of flags - El Forum - 02-09-2010

[eluser]ocdcoder[/eluser]
Yea, I imagine its not THAT much of a performance tweak, but if you have any large forms with multiple validation rules (possible for some web apps) then it could be noticeable.

It was part for performance and the other part convention. PHP uses a FLAG system and I've seen other frameworks use object/array parameters (think jQuery). Although I didn't state it, and I guess I should have. I was curious if this was the "CodeIgniter" way to pass multiple parameters as one.

I'm relatively new to CodeIgniter and I want to keep my code, and how I do things, relatively conventional to the framework. Hence my curiosity for something that would seem so minor.


Why use strings when doing rules for Form Validation instead of flags - El Forum - 02-09-2010

[eluser]bretticus[/eluser]
Understandable. I thought it was an interesting way to handle validation myself. I have been using CI for only a year or so now. At first I thought it was "cludgy", but then the whole chaining of rules together in one call seems like a very intuitive way to stack rules together. I am curious how the "other frameworks" you mentioned handle validation rules.

But yes, CI has one validation class that it "ships" with. One thing I really like about CI is that you can very easily build your own validation (or anything else) class (and I'm sure this is a hallmark of pretty much all other frameworks too.) I've only seen this string-based parameter approach used with the Form_validation class if that answers your question.


Why use strings when doing rules for Form Validation instead of flags - El Forum - 02-09-2010

[eluser]ocdcoder[/eluser]
The "other frameworks" wasn't so much about validation in other PHP frameworks, but about frameworks dealing with "dynamic" parameters. jQuery, being js and not php, uses objects like: $.func({param1:val, param2:val}); I have also seen (and used by preference) php this way as well:

Code:
myFunc(array('param1'=>val, 'param2'=>val));

or (to put in form validation terms):

Code:
$this->form_validation->set_rules("email", "email", array('trim', 'required', 'valid_email'));

for example.

I guess I'm just not a big fan of playing with strings like they're arrays (even though they technically are, but you know what i'm talking about). Oh well, I may go modify the library later, but not right now. I was more just curious about the coding convention than anything else. Thanks for the replies!


Why use strings when doing rules for Form Validation instead of flags - El Forum - 02-09-2010

[eluser]bretticus[/eluser]
I'm not a fan of using parse-able strings as arguments either (Then again I don't like using arrays as arguments either. I like the optional mutiple arguments model...think printf, etc. or even func_get_args) However, I suspect the CI author(s) were attempting to save a few key strokes. Smile