CI 2.2 Form Validation not working as expected |
This is CI 2.2.3
I'm using an array for the name of a set of input values 'opt' (ie opt[1] opt[2] ...). Here is a dump of the validation config array passed to form_validation->set_rules(). As you can see the some of the input are required but fields Email and Phone are not. Email is using a CI supplied function (valid_email) while Phone is using a function in our MY_form_validation class. If no input is supplied for the Email and Phone input field no error should be issued, but one is. This looks like a bug to me. TEST - 2015-10-18 10:16:34 --> Array ( [0] => Array ( [field] => opt[1] [label] => Team Manager Name [rules] => trim|required|max_length[50] ) [1] => Array ( [field] => opt[2] [label] => Team Kind [rules] => trim|required|max_length[50] ) [2] => Array ( [field] => opt[4] [label] => Email [rules] => trim|valid_email ) [3] => Array ( [field] => opt[3] [label] => Phone [rules] => trim|phone ) )
You are applying two rules to each of the phone and email fields. Trim will not return an error but you also have valid_email, and blank is not a valid email. I have no idea what the rule 'phone' is. I presume you have a custom rule somewhere?
What is the error message you are getting?
The email rule should not return any error as there is no "required" rule in there. The only problem should be with the phone rule.
Website: http://avenir.ro
(10-18-2015, 11:48 PM)Avenirer Wrote: The email rule should not return any error as there is no "required" rule in there. The only problem should be with the phone rule. My understanding is the same as yours. You add "required" if an empty response should be an error. phone is a rule I have added that matches a US phone number, much like valid_email matches an email address. Both valid_email and phone report an invalid email and invalid phone number when given blank input IN THIS CONTEXT. Forms elsewhere on the site that use these rules do not report an error. What seems to be unique to the non-working case is that the "variable" name is an array element (eg opt[3]) rather than a scalar (eg xxx). I guess I need to code up an example to illustrate what I think is erroneous behavior. I'll look into it at some point.
10-26-2015, 11:50 PM
(This post was last modified: 10-27-2015, 12:01 AM by joseph.dicdican. Edit Reason: Edition on input pattern for phone )
Hi,
Based on what I understood. Quote:If no input is supplied for the Email and Phone input field no error should be issued, but one is. You want to validate email and phone if they are filled and if not, there should be no validation. If that's the case, just a suggestion, for me, I will set the validation for email and phone after submitting the form. By checking if those two inputs are not null, then I will set the validation respectively. PHP Code: public function methodName() There are also other way to validate your email and phone. If you like a quick validation, you can use the HTML5 input types. For email, you can use type email. For phone validation, you can also use pattern in validation. Please refer here : http://www.w3schools.com/tags/att_input_pattern.asp Code: <input type="email" name="email" /> These are just options, it depends on you what is best fitted for the project you are working on. Thank you. Hope I helped you somehow. - joseph.dicdican ![]()
Joseph K. Dicdican
Softshore Global Solutions Inc. | Junior Web Developer Intern Passerelles numériques Philippines | Scholar [email protected]
Joseph,
Thanks for the suggestions. All that you wrote are ways around what I think is a bug. Since the problem is (I believe) related to $_POST variables that are PHP arrays (something I don't use all that often), I coded around it with additional validation functions. For example adding opt_valid_email() that tests for an empty input before calling valid_email. SRG
10-27-2015, 08:37 AM
(This post was last modified: 10-27-2015, 08:54 AM by joseph.dicdican. Edit Reason: code indention )
SRG,
I apologize bout the previous post, I didn't read it carefully. Now I got you somehow, you are using array for input Code: <form action="<?php echo base_url('test/index'); ?>" method="post"> I coded this on my local and had it running. PHP Code: public function index() In this part, I enclosed the index value of each opt with parenthesis "()" to obtain the integer value. The value of index depends on the arrangement of inputs on view page. Code: $config = array( If we write it merely like below, Code: $config = array( Additionally, based on what I just learned, we do not need to check if input is not null before setting validation rules, we can just set a validation rule directly without required in rules. This validates the specific field if it is not null. You can see the attachment what I got in my testing. By the way, I did not include the phone validation for this is just for testing. I hope this time this can help. Thank you. - joseph.dicdican ![]()
Joseph K. Dicdican
Softshore Global Solutions Inc. | Junior Web Developer Intern Passerelles numériques Philippines | Scholar [email protected] (10-21-2015, 12:37 PM)SRGreenwood Wrote:(10-18-2015, 11:48 PM)Avenirer Wrote: The email rule should not return any error as there is no "required" rule in there. The only problem should be with the phone rule. Now I understand what you mean. You should try this ``` $this->form_validation->set_rules('phone[]','Phone','your_validation_conditions_here'); Website: http://avenir.ro
|
Welcome Guest, Not a member yet? Register Sign In |