i tried to save account to the db.
when validating the form with form_validation, it always returning false, even when i tried to insert the correct value. heres the code
public function save(){
$params = $this->input->post();
if(!empty($params['id_account']) && !empty($params['password']) ){
$this->form_validation->set_rules('confPass', 'Confirmation Password', 'matches[password]');
}else{
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('confPass', 'Confirmation Password', 'required|matches[password]');
}
$this->form_validation->set_data($params);
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('id_role', 'Role', 'required');
if ($this->form_validation->run() == TRUE) {
unset($params['confPass']);
$params['account_status'] = array_key_exists('account_status', $params) ? $params['account_status'] : ACCOUNT_STATUS_PENDING;
$this->load->model('user_model');
$this->user_model->save($params);
$this->session->set_flashdata('notif', '<div class="alert alert-success">Berhasil menyimpan data.</div>');
redirect('rbac/form/account');
} else {
$error = validation_errors();
echo '<pre>';
print_r($error);
echo '</pre>';
var_dump($params['password']);
var_dump($params['confPass']);
die();
// $this->session->set_flashdata('notif', '<div class="alert alert-danger">'.$error.'</div>');
// redirect('rbac/form/account');
}
}
i tried to returning the validation_errors(), $params['confPass'] & $params['password'] and here is the result :
The Confirmation Password field does not match the password field.
string(1) "e" string(1) "e"
as you can see $params['confPass'] and $params['password'] is match.
if(!empty($params['id_account']) && !empty($params['password']) ){
$this->form_validation->set_rules('confPass', 'Confirmation Password', 'matches[password]');
}else{
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('confPass', 'Confirmation Password', 'required|matches[password]');
}
When the condition you wrote above is true i.e. control is in if (below) block
if(!empty($params['id_account']) && !empty($params['password']) )
you did not put a condition for the 'password' field you only have confirm password rule. Which does not match to anything and although the passwords match it always return false.
You need to add validation rule for password
$this->form_validation->set_rules('password', 'Password', 'required');
before confirm password rule which is looking to match the password.
If your condition states that it does not require password you can do something like,
$this->form_validation->set_rules('password', 'Password', 'min_length[5]');
anything which fulfills your condition but not "required" i.e trim or min_length
Hope it helps.
Related
I have a input page with 3 inputs username, password, OTP. When password is wrong i want to send it 1 function and if OTP fails i want to send it other function. How can i achieve this. Below is a sample code.
public function verifyLogin() {
$data['BASE_URL'] = base_url();
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
$this->form_validation->set_rules('otp', 'OTP', 'trim|xss_clean|callback_check_otpNum');
if ($this->form_validation->run() == false) {
if(//passwordFails)
$this->index();
else if(//otpfails)
$this->loginPage();
}
}
Try using $this->form_validation->error_array() to get all failed validations and then equating accordingly
I used to validate my registration form by the following code:
$this->form_validation->set_rules('password', 'Password', 'trim|required|matches[cpassword]|md5');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim|required');
but when i browse my form it shows me an error that password doesn't match.
codeigniter form result
Controllers are not for database. So, please change your code to this:
$this->form_validation->set_rules('password', 'Password', 'trim|required');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim|required|matches[password]');
If you want to put it as a hash into the database, you should use Models. Here is an example code:
public function signup() {
$password = $this->input->post('password', true);
$hash = password_hash($password, PASSWORD_BCRYPT); // put $hash variable into your database
...
}
<?php
class Form extends CI_Controller {
public function index()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('confpassword', 'Password', 'required|matches[password]', 'callback__matcherror');
//$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
//$this->form_validation->set_rules('email', 'Email', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('login');
}
else
{
$this->load->view('insert_dream');
}
}
public function _matcherror() {
$this->form_validation->set_message('_matcherror', 'Passwords should match');
return FALSE;
}
}
?>
i am a newbie to codeigniter. The above code doesnt display passwords should match error message. Is something wrong with the callback or Am i missing something.
Take a look here. You don't need to make a callback.
You are passing callback__matcherror as fourth parameter of set_rules function.It should be 3rd parameter. Use this way
$this->form_validation->set_rules('confpassword', 'Password', 'required|matches[password]|callback__matcherror');
Note
You will get this error message if your password fields match.Because you applying 3 rule there.3rd rule(call_back_function) will apply when 2nd rule is success. Your 2nd rule will valid when passwords matches.
matches[password]
will automatically check for password. You need not to use callback function callback__matcherror
I have used codeigniter code in the below when I create a user if it is already exist it should display a message duplicate value but it displays 1062 error. Pls help to solve the issue.
Controller
function create_member()
{
$this->load->library('form_validation');
// field name, error message, validation rules
$this->form_validation->set_rules('first_name', 'Name', 'trim|required');
$this->form_validation->set_rules('last_name', 'Last Name', 'trim|required');
$this->form_validation->set_rules('email_address', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');
if($this->form_validation->run() == FALSE)
{
$this->load->view('signup_form');
}
else
{
$this->load->model('membership_model');
if($query = $this->membership_model->create_member())
{
$data['main_content'] = 'signup_successful';
$this->load->view('includes/template', $data);
}
else
{
$this->load->view('signup_form');
}
}
}
Model
function create_member()
{
$new_member_insert_data = array(
'first_name' => $this->input->post('first_name'),
'last_name' => $this->input->post('last_name'),
'email_address' => $this->input->post('email_address'),
'username' => $this->input->post('username'),
'password' => md5($this->input->post('password'))
);
$insert = $this->db->insert('membership', $new_member_insert_data);
if ($this->db->_error_number() == 1062)
{
$this->session->set_flashdata('duplicate_email', 'Duplicate value');
redirect('login');
}
return $insert;
}
It seems that your error is because your database is not allowing to add multiple entries with same username/email, for this you need to add codeigniter is_unique validation on that particular field. Below is example for codeigniter unique validation : $this->form_validation->set_rules('username', 'Username', 'required|min_length[5]|max_length[12]|is_unique[membership.username]');
Try to use $this->db->query instead of insert and create a SQL statement using INSERT IGNORE and you can handle the duplicates silently.
Why just not do a previous query to check if the email exists aleady ?
Anyway, you can do something like that:
$query_string = $this->db->insert_string('users', $data);
$this->db->query($query_string.' ON DUPLICATE KEY UPDATE id=id');
// or
$this->db->query(str_replace("INSERT INTO", "INSERT IGNORE INTO", $query_string));
if( $this->db->affected_rows() == 0 ) {
//duplicate email
}
Because of the bug in AngularJS, I need more than ever to make CodeIgniter validate my registration form.
I have done everything I can think of to troubleshoot this problem. Maybe it just needs a different set of eyes? Or maybe someone has dealt with this before?
public function register_validation() {
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'Username',
'required|trim|is_unique[users.username]|max_length[32]');
$this->form_validation->set_rules('password', 'Password',
'required|trim|min_length[6]');
$this->form_validation->set_rules('email', 'Email',
'required|trim|valid_email|is_unique[users.email]');
if($this->form_validation->run()) {
echo "Pass!";
} else {
echo "NOPE!";
$this->load->view('home');
}
}
This line:
$this->form_validation->set_rules('username', 'Password',
'required|trim|min_length[6]');
Should be:
$this->form_validation->set_rules('password', 'Password',
'required|trim|min_length[6]');
You used username twice. You should change your run() check to:
if($this->form_validation->run() == FALSE))
{
echo "NOPE!";
$this->load->view('home');
}
else
{
echo "Pass!";
}
this line is correct
$this->form_validation->set_rules('username', 'Username',
'required|trim|is_unique[users.username]|max_length');
first username is your field name and second one is which show in the error.
but this line
this->form_validation->set_rules('username', 'Password',
'required|trim|min_length[6]');
here you used username name which is already validating instead of username you used password
there are many ways for applying conditions
first one is
if($this->form_validation->run() == FALSE)) {
$this->load->view('home');
} else {
echo "Pass!";
}
second one is
if($this->form_validation->run() != FALSE) {
echo "Pass!";
} else {
$this->load->view('home');
}
Change
if($this->form_validation->run()) {
to
if($this->form_validation->run($this)) {