Je construis un blog avec CodeIgniter 3.1.3 et j'ai une fonctionnalité dans l'administrateur pour ajouter de nouveaux utilisateurs. Lorsque j'essaie d'ajouter un nouvel utilisateur, le message d'erreur suivant s'affiche:
Le champ Mot de passe ne correspond pas au champ confirm_password.
bien que le mot de passe donné dans mot de passe et mot de passe confrom soit le même.
Je ne peux pas comprendre ce qui cause le problème.
Voici mon modèle:
public function insert($data){
$this->db->insert('users', $data);
return true;
}
Voici mon contrôleur
public function add(){
//Validation Rules
$this->form_validation->set_rules('first_name','First Name','trim|required');
$this->form_validation->set_rules('last_name','Last Name','trim|required');
$this->form_validation->set_rules('email','Email','trim|required|valid_email');
$this->form_validation->set_rules('username','Username','trim|required|min_length[3]');
$this->form_validation->set_rules('password', 'Password', 'required|matches[confirm_password]');
$data['groups'] = $this->User_model->get_groups();
if($this->form_validation->run() == FALSE){
//Views
$data['main_content'] = 'admin/users/add';
$this->load->view('admin/layouts/main', $data);
} else {
//Create Data Array
$data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'username' => $this->input->post('username'),
'password' => md5($this->input->post('password')),
'group_id' => $this->input->post('group'),
'email' => $this->input->post('email')
);
//Table Insert
$this->User_model->insert($data);
//Create Message
$this->session->set_flashdata('user_saved', 'User has been saved');
//Redirect to pages
redirect('admin/users');
}
Voici mon avis:
<div class="row">
<div class="col-lg-12">
<div class="form-group">
<label>First Name</label>
<input class="form-control" type="text" name="first_name" value="<?php echo set_value('first_name'); ?>" placeholder="Enter First Name" />
</div>
<div class="form-group">
<label>Last Name</label>
<input class="form-control" type="text" name="last_name" value="<?php echo set_value('last_name'); ?>" placeholder="Enter Last Name" />
</div>
<div class="form-group">
<label>Email Address</label>
<input class="form-control" type="email" name="email" value="<?php echo set_value('email'); ?>" placeholder="Enter Email" />
</div>
<div class="form-group">
<label>Username</label>
<input class="form-control" type="text" name="username" value="<?php echo set_value('username'); ?>" placeholder="Enter Username" />
</div>
<div class="form-group">
<label>Password</label>
<input class="form-control" type="password" name="password" value="<?php echo set_value('password'); ?>" placeholder="Enter Password" />
</div>
<div class="form-group">
<label>Confirm Password</label>
<input class="form-control" type="password" name="confirm_password" value="<?php echo set_value('confirm_password'); ?>" placeholder="Confirm Password" />
</div>
<div class="form-group">
<label>User Group</label>
<select name="group" class="form-control">
<?php foreach($groups as $group) : ?>
<option value="<?php echo $group->id; ?>"><?php echo $group->name; ?></option>
<?php endforeach; ?>
</select>
</div>
</div><!-- /.row -->
$this->form_validation->set_rules('first_name','First Name','trim|required');
$this->form_validation->set_rules('last_name','Last Name','trim|required');
$this->form_validation->set_rules('email','Email','trim|required|valid_email');
$this->form_validation->set_rules('username','Username','trim|required|min_length[3]');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('confirm_password', 'Confirm Password', 'required|matches[password]');
Changez votre validation de formulaire en ceci et n'oubliez pas d'activer le chargement automatique de la validation de formulaire dans le fichier config/autoload.php tel que $autoload['libraries'] = array('form_validation');
. Si cela ne fonctionne pas, veuillez commenter ci-dessous.
config/autoload.php:
$autoload['libraries'] = array('form_validation');
Vue:
<?php echo form_label('Password:'); ?>
<?php echo form_input(array('placeholder'=>'Enter your password', 'type'=>'password', 'name'=>'password')); ?>
<?php echo form_label('Confirm password:'); ?>
<?php echo form_input(array('placeholder'=>'Confirm your password', 'type'=>'password', 'name'=>'confirm_password')); ?>
Manette:
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('confirm_password', 'Confirm password', 'required|matches[password]');
Vous devez utiliser les règles définies deux fois pour le double mot de passe. Confirmation
$this->form_validation->set_rules('username','Username','trim|required|min_length[3]|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'required|matches[confirm_password]');
http://prntscr.com/kl4mfu
http://prntscr.com/kl4m7u
http://prntscr.com/kl4lvm