Welcome Guest, Not a member yet? Register   Sign In
Form validation with non-required fields
#1

[eluser]Mitchell[/eluser]
I need a quick sanity check. I'm using the form validation class with V 1.7.1.

If I have non-required fields with rules set such as "numeric" or "valid_email," then I get a form validation error such as "The Product Number field must contain only numbers" when the field is left blank.

It seems to me that if the field is NOT required and the field is left blank, then the "numeric" and other similar rules should not return false. Am I missing something or is this the way it's supposed to work?
#2

[eluser]Stelian Mocanita[/eluser]
You could add a precondition like trim() which would make CI validate on your behalf. Works like a charm
#3

[eluser]Mitchell[/eluser]
Stelian,

Thanks for your reply, but I am using trim. I just checked and the field has:

'rules' => 'trim|numeric'

If the field is left blank, I get "The Number of Pages field must contain only numbers."

Unless I'm missing something, I may have to go and overload all the functions that check the fields.
#4

[eluser]Stelian Mocanita[/eluser]
This doesn't make much sense since the validation of numeric does not include a string length check, will double check it asap and come back with a reply maybe without touching those methods in the form_validation library
#5

[eluser]theshiftexchange[/eluser]
I've got this working.

I have an optional 'email' field. I only get a "must be valid email" error IF the field has something in it. i.e. if the user leaves the field blank, its ok.

Can we see your code to work out why it is not working? It should be without overloading...
#6

[eluser]Mitchell[/eluser]
Your reply prompted me to go back and look through my code, and I found a couple of lines I had changed in order to do further form validation. These changes caused the form input to not appear as an empty string. So I need to make some changes to my changes.

Thanks for taking the time to reply.

Mitchell




Theme © iAndrew 2016 - Forum software by © MyBB