Welcome Guest, Not a member yet? Register   Sign In
Weird form validation problem
#1

[eluser]iainco[/eluser]
The error I get when my password fields don't match is:

Code:
The Confirm Password field does not match the password1 field.

but I've set the label correctly for password1, any ideas? Controller code below, view code is perfect - field names matching up and so on.

This also happens with the email1 and email2 fields...

Code:
<?php if(!defined('BASEPATH')) exit('No direct script access allowed.');
    
class Register extends Base_Controller
{
    function index()
    {
        $this->display('register', array('title' => 'Register'));
    }
        
    function submit()
    {
        $this->form_validation->set_rules(array(
            array(
                'field' => 'title',
                'label' => 'Title',
                'rules' => 'required|trim|min_length[2]|max_length[4]'
            ),
            array(
                'field' => 'forenames',    
                'label' => 'Forename(s)',            
                'rules' => 'required|trim|max_length[30]'
            ),
            array(
                'field' => 'surname',    
                'label' => 'Surname',                
                'rules' => 'required|trim|max_length[30]'
            ),
            array(
                'field' => 'postcode',    
                'label' => 'Postcode',                
                'rules' => 'required|trim'
            ),
            array(
                'field' => 'day',        
                'label' => 'Date of Birth Day',    
                'rules' => 'required|trim|is_natural_no_zero|max_length[2]'
            ),
            array(
                'field' => 'month',        
                'label' => 'Date of Birth Month',    
                'rules' => 'required|trim|is_natural_no_zero|max_length[2]'
            ),
            array(
                'field' => 'year',        
                'label' => 'Date of Birth Year',    
                'rules' => 'required|trim|is_natural_no_zero|exact_length[4]'
            ),
            array(
                'field' => 'sex',        
                'label' => 'Sex',                    
                'rules' => 'required|trim|is_natural|exact_length[1]'
            ),
            array(
                'field' => 'email1',        
                'label' => 'E-mail Address',                
                'rules' => 'required|trim|max_length[30]|valid_email'
            ),
            array(
                'field' => 'email2',        
                'label' => 'Confirm E-mail Address',                
                'rules' => 'required|matches[email1]|callback__emailCB'
            ),
            array(
                'field' => 'password1',    
                'label' => 'Password',            
                'rules' => 'required|trim|min_length[6]|max_length[12]'
            ),
            array(
                'field' => 'password2',    
                'label' => 'Confirm Password',            
                'rules' => 'required|matches[password1]'
            ))
        );
        
        if($this->form_validation->run())
        {
            $this->load->model('user/register_model');
            $this->register_model->register();
            redirect();        
        }
        else
        {
            $this->load->helper('form');
            show_error(validation_errors());
        }    
    }

    function _emailCB()
    {
        $this->load->model('user/register_model');
        $this->form_validation->set_message('_emailCB', 'The e-mail address you entered is currently in use with another account.');
        return $this->register_model->emailCB();
    }
}
?>
#2

[eluser]Teks[/eluser]
Are the fields using the correct names in the view file? - if the form fields in the view have different names to these, you'd get this error.

Also: it might be simpler to put the 'validation_errors()' function directly in the view, as shown in the user guide.
#3

[eluser]iainco[/eluser]
Sorry, I've not been working on this problem for a while but just started.

The field names in the view are exactly the same as the field names I've setup for the validation.

Strange little problem but still can't get it fixed.

This is my updated code:

Code:
<tr>
                        <td><label for="email1">E-mail Address</label></td>
                        <td>
                            &lt;input id="email1" name="email1" type="text" class="fv['email']" value="&lt;?php echo set_value('email1');?&gt;" /&gt;
                            &lt;?php echo form_error('email1');?&gt;
                        </td>
                    </tr>
                    <tr>
                        <td><label for="email2">Confirm E-mail Address</label></td>
                        <td>
                            &lt;input id="email2" name="email2" type="text" class="fv['']" value="&lt;?php echo set_value('email2');?&gt;" /&gt;
                            &lt;?php echo form_error('email2');?&gt;
                        </td>
                    </tr>
                    <tr>
                        <td><label for="password1">Password</label></td>
                        <td>
                            &lt;input id="password1" name="password1" type="password" class="fv['password']" value="&lt;?php echo set_value('password1');?&gt;" /&gt;
                            &lt;?php echo form_error('password1');?&gt;
                        </td>
                    </tr>
                    <tr>
                        <td><label for="password2">Confirm Password</label></td>
                        <td>
                            &lt;input id="password2" name="password2" type="password" class="fv['']" value="&lt;?php echo set_value('password2');?&gt;" /&gt;
                            &lt;?php echo form_error('password2');?&gt;
                        </td>
                    </tr>

Code:
$this->form_validation->set_rules(array(
                        [removed]

            array(
                'field' => 'email1',
                'label' => 'E-mail Address',
                'rules' => 'trim|required|max_length[50]|valid_email|xss_clean'
            ),
            array(
                'field' => 'email2',        
                'label' => 'Confirm E-mail Address',                
                'rules' => 'matches[email1]|callback__emailCB'
            ),
            array(
                'field' => 'password1',    
                'label' => 'Password',            
                'rules' => 'min_length[6]|max_length[12]|xss_clean'
            ),
            array(
                'field' => 'password2',    
                'label' => 'Confirm Password',            
                'rules' => 'matches[password1]'
            ))
        );
        
                [removed]
        
        if ($this->form_validation->run()) {
                        $this->display('home');
        }
        else {
            $this->display('register');
        }

Any clues would be really helpful.

PS. I have not modified the "matches" error message with the set_message function.




Theme © iAndrew 2016 - Forum software by © MyBB