Form validation don't work - pippuccio76 - 06-20-2017
Hi this is my form user/register :
Code: <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>Register</h1>
<?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(); ?>">
<p>
<?= form_label('Email*:' ,'email') ;?>
<?= form_input('email' ,set_value('email'),'id="email_id"') ;?>
</p>
<p>
<?= form_label('Username:' ,'username') ;?>
<?= form_input('username' ,set_value('username'),'id="username_id"') ;?>
</p>
<p>
<?= form_label('Password*' ,'password') ;?>
<?= form_input('password' ,'','id="password_id"') ;?>
</p>
<p>
<?= form_label('Repeat Password*' ,'repeat_password') ;?>
<?= form_input('password' ,'','id="password_id"') ;?>
</p>
<?= form_submit('submit','Login') ;?>
<?= form_close();?>
<?php echo form_close();?>
<div><?= validation_errors(); ?></div>
</body>
</html>
This is my controller function :
Code: function register(){
$this->load->library('form_validation');
$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('password','Password','required|min_length[6]');
$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'));
$this->load->model('user_model');
$data=[];
if($this->form_validation->run()){
$res=$this->user_model
->registerNewUser(
$this->input->post('email'),
$this->input->post('username'),
$this->input->post('password'),
$this->input->post('repeat_password')
);
$data['res']=$res;
if($res!=FALSE){
$_SESSION['user_id']=$res->id;
redirect('user/prova');
}
}
$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 :
http://lemuria.altervista.org/user_codeigniter/index.php/user/register
RE: Form validation don't work - Wouter60 - 06-20-2017
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.
RE: Form validation don't work - Martin7483 - 06-20-2017
As Wouter60 pointed out, both fields are named password
Code: <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
Code: <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', 'Password', 'required'); $this->form_validation->set_rules('password_repeat', 'Password Confirmation', 'required|matches[password]');
Example from documentation
https://www.codeigniter.com/user_guide/libraries/form_validation.html#cascading-rules
|