Welcome Guest, Not a member yet? Register   Sign In
Improper usage or bug - From validation - trim|min_length
#1

[eluser]kdawson[/eluser]
Hi,
I am using the form validation like this
Code:
$this->form_validation->set_rules('home_phone', 'Home Phone', 'trim|max_length[20]|min_length[10]|alpha_dash');

Yet in that form field, if the user leaves it blank (not required) but also has a blank space in there, it triggers the "min_length[10]" error.

Shouldn't it be trimming first? I notice when it directs back to the form and using set_value() , the space is removed.

I thought maybe having the "trim|" first in the rules would help, but no.

Should the fact that it is not required and trimmed mean that:
- It is first trimmed (white space removed)
- Checked if it is required or not
- if it is not required and blank after trimming, it should pass the rule "trim|max_length[20]|min_length[10]|alpha_dash"


Am I doing something wrong?


Thanks

Regards,
Ken
#2

[eluser]bEz[/eluser]
Huh?

'min_length' is requiring that the value have at the very least ( 10 ) characters... So having a 'blank' value, meanwhile not "required" by clause indirectly requires a value by means of the min_length validation clause.
#3

[eluser]kdawson[/eluser]
Well...
It does pass the rules, if there is no blank space, that part makes sense to me. i.e.

- The field is NOT required so blank is fine
- if NOT blank, then the other rules apply.

That much makes sense.

It is apparently the sequence of the trim that is the issue here... I think, and that is what I am asking about.

Should it not
- trim first
- then check if it is required
- then if any value is present, apply the other rules.

?
#4

[eluser]oneos[/eluser]
The way CI does it is something like this:

If the field is empty, don't run any rules unless the 'required' rule is present. At that point it obviously fails on required. In this case, the empty field isn't passing the rules, it just isn't getting tested.

When the field isn't empty, it gets run against all the rules (shortcutting of course). Your trim is doing what you expect, trimming it prior to hitting min_length[]; however, min_length[] was never passing an empty field.


A quick thought, following your logic, could be a rule like so:

Code:
'trim|callback__pad_empty|max_length[20]|min_length[10]|trim|alpha_dash'

function _pad_empty($str)
{
    if ($str == '')
        return str_pad('', 10);
    return $str;
}

So in this case, whitespace input will be trimmed down to nothing, padded with 10 spaces, pass the tests, and then trimmed back down to nothing (as to not end up with 10 spaces on reload).
#5

[eluser]kdawson[/eluser]
I guess I don't understand why it passes the test when there is not any white space in the field?
#6

[eluser]oneos[/eluser]
Because like I said, it doesn't pass the test, it simply isn't tested. (the rules are never run when the field is blank since its not a required field)
#7

[eluser]kdawson[/eluser]
I don't know if I just need some sleep or what, but...

Isn't that what we want? To just ignore a blank space or two if that is all that is in the field?


If it trimmed first (and removed the blank spaces), then it would just see the field as empty?

And then as you said "In this case, the empty field isn’t passing the rules, it just isn’t getting tested."

It seems to me that would be the priority, to first remove the blanks to see if there is anything there worth testing.

It just doesn't seem correct to me that it would run any test on a bank space alone in a text field. Obviously the user does not intend to submit a blank space, so the field being trimmed first removes a blank space and returns an empty field that will not be tested if not required.


Regards,
Ken
#8

[eluser]kdawson[/eluser]
How about this:

Should the validator test a field that is empty but has a blank space in it?

Or should it consider the field empty?
#9

[eluser]oneos[/eluser]
What happens then, in a situation where you do want some combination of whitespace as a submission? The validator can't make that decision for you.

Trimming it first (as a validation rule) does not count as an empty field. The field had data in it, and thus the validator rightly initiated the ruleset.

If you don't want to do the extra trim and callback and all that craziness, treat the fields before running validation (or perhaps even run two different rulesets).




Theme © iAndrew 2016 - Forum software by © MyBB