Welcome Guest, Not a member yet? Register   Sign In
CI 2.2 Form Validation not working as expected
#1

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
       )
)
Reply
#2

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?
Reply
#3

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.
Reply
#4

(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.
Reply
#5

(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() 
{
 
   $this->load->library('form_validation');
 
   $this->form_validation->set_rules('firstname''First Name''required|xss_clean');
 
   
    
// set validation rules for email or phone if not null
 
   if($_POST['email'] != null || $_POST['phone'] != null) { 
 
       $this->form_validation->set_rules('email''Email''trim|required|valid_email|xss_clean');
 
       $this->form_validation->set_rules('phone''Phone''trim|required|some_regex_validation|xss_clean');
 
   }

 
   if($this->form_validation->run()) {
 
       //do your process here
 
   }


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" />
<input type="text" name="phone" pattern="^d{2,3}-d{4}$" title="Please follow the format. (e.g. 032-0000)" />


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  Big Grin
Joseph K. Dicdican
Softshore Global Solutions Inc. | Junior Web Developer Intern
Passerelles numériques Philippines | Scholar
[email protected] 
Reply
#6

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
Reply
#7

(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">
   <p>
       <label>Name</label>
       <input type="text" name="opt[]" value="<?php echo set_value('opt['.(0).']'); ?>" />
   </p>
   <p>
       <label>Email</label>
       <input type="text" name="opt[]" value="<?php echo set_value('opt['.(1).']'); ?>" />
   </p>
   <p>
       <label>Phone</label>
       <input type="text" name="opt[]" />
   </p>
   <p>
       <input type="submit" value="Submit" />
       <a href="<?php echo base_url('test/index'); ?>">reset</a>
   </p>
</form>
<?php echo validation_errors(); ?>
I have searched from the internet, also from the user_guide of codeigniter. I found out that we can set validation rules on input arrays by using their indexes or the whole input name array.

I coded this on my local and had it running.
PHP Code:
public function index() 
{
 
   $this->load->library('form_validation');
 
   
    
// I hard coded the index number for each option. (e.g. opt[0] for Name and opt[1] for email
 
   // Enclose it with parenthesis "()" to obtain the integer value
 
   $config = array(
 
       array('field' => 'opt['.(0).']''label' => 'Name''rules' => 'trim|required|max_length[50]'),
 
       array('field' => 'opt['.(1).']''label' => 'Email''rules' => 'trim|valid_email')
 
   );
 
   
    
// setting rules
 
   $this->form_validation->set_rules($config);
 
 
   // intended for debugging
 
   $this->prePrintR($config);
 
  
    if
($_POST) {
 
       $input $this->input->post();
 
       $this->prePrintR($input);
 
   }
 
   
    
// checking if form is valid else, load the view and show validation_errors in test/index
 
   if($this->form_validation->run()) {
 
       echo 'form is valid';
 
   

 
   $this->load->view('test/index');
}

// Customized debugger to print_r variables
private function prePrintR($expression
{
 
   echo '<pre>';
 
   echo print_r($expression);
 
   echo '</pre>';


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(
   array('field' => 'opt['.(0).']', 'label' => 'Name', 'rules' => 'trim|required|max_length[50]'),
   array('field' => 'opt['.(1).']', 'label' => 'Email', 'rules' => 'trim|valid_email')
);

If we write it merely like below,
Code:
$config = array(
   array('field' => 'opt[0]', 'label' => 'Name', 'rules' => 'trim|required|max_length[50]'),
   array('field' => 'opt[1]', 'label' => 'Email', 'rules' => 'trim|valid_email')
);
then PHP by default considers them as strings not with index. I suppose these cause the errors. 
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  Smile
Joseph K. Dicdican
Softshore Global Solutions Inc. | Junior Web Developer Intern
Passerelles numériques Philippines | Scholar
[email protected] 
Reply
#8

(This post was last modified: 10-27-2015, 01:22 PM by Avenirer.)

(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.

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.

Now I understand what you mean. You should try this
```
$this->form_validation->set_rules('phone[]','Phone','your_validation_conditions_here');
Reply




Theme © iAndrew 2016 - Forum software by © MyBB