Welcome Guest, Not a member yet? Register   Sign In
Form validation don't work

Hi this is my form user/register :
<!DOCTYPE html>

       <meta charset="UTF-8">
       <?php   echo form_open('user/register'); ?>
       <input type="hidden" name="<?php echo $this->security->get_csrf_token_name(); ?>" value="<?php echo $this->security->get_csrf_hash(); ?>">

           <?= form_label('Email*:' ,'email') ;?>
           <?= form_input('email' ,set_value('email'),'id="email_id"') ;?>
           <?= form_label('Username:' ,'username') ;?>
           <?= form_input('username' ,set_value('username'),'id="username_id"') ;?>
           <?= form_label('Password*' ,'password') ;?>
           <?= form_input('password' ,'','id="password_id"') ;?>
           <?= form_label('Repeat Password*' ,'repeat_password') ;?>
           <?= form_input('password' ,'','id="password_id"') ;?>
           <?= form_submit('submit','Login') ;?>
           <?= form_close();?>
       <?php  echo form_close();?>
       <div><?= validation_errors(); ?></div>


This is my controller function :

function register(){
      $this->form_validation->set_rules('email','Email','required|valid_email', array('required' => 'Inserisci la mail','valid_email'=>'la mail deve essere valida'));
      $this->form_validation->set_rules('repeat_password','Password Confirmation','required');    
      $this->form_validation->set_rules('username','Username','min_length[6]|max_length[12]',array('min_length'=>'Lunghezza minima 6 caratteri', 'max_length'=>'Lunghezza massima 12 caratteri'));
       $data['errors']    = $this->user_model->errors;
       $data['messages']  = $this->user_model->messages;
       $this->load->view('user/register', $data);

But if i insert email  and password i have this error :

The Password field is required.

if i insert password and password_repeat to i have this error :

The Password Confirmation field is required.

this is a test application :


Your form has two input fields with the name 'password'. If your form is posted, and the second field with the name 'password' is empty, form validation will come up with an error message. You should rename the second field to 'repeat_password'.
And (less important, but I need to mention it), your form is being closed twice.

(This post was last modified: 06-20-2017, 01:12 PM by Martin7483.)

As Wouter60 pointed out, both fields are named password

<input name="password" value="" id="password_id" type="text">
<input name="password" value="" id="password_id" type="text">

Both fields also have the same ID. ID's in HTML must always be unique.
Besides being required, repeat password should also be set to match the password field

<input name="password" value="" id="password_id" type="text">
<input name="password_repeat" value="" id="password_repeat_id" type="text">

And set your rules like this
PHP Code:
$this->form_validation->set_rules('password_repeat''Password Confirmation''required|matches[password]'); 

Example from documentation

Theme © iAndrew 2016 - Forum software by © MyBB