Codeigniter Form Validation Rule for match (password) - php

I am trying to write Form validation rules in my Controller to submit Change Password form in which I am checking the old password too. I am getting the old password(current) from db and placing it in a hidden input field.
My Rules are simple and are given below
$config=array(
array(
'field' => 'old_password',
'label' => 'oldpass',
'rules' => 'trim|required'
),
array(
'field' => 'conf_password',
'label' => 'connewpass',
'rules' => 'trim|required|matches[password]'
),
array(
'field' => 'password',
'label' => 'newpass',
'rules' => 'trim|required'
)
My hidden input field in the form to save current password is like
<input type="hidden" name="old_pass" value="<?php echo $user['password']?>">
I know that matches(field name) in rules work for matching two field values but Where I am stuck is that the password coming from db is md5 encrypted. How can I encrypt the password coming from form and match with old pass field in the rule?

There is no need of putting old password hash in hidden field. it's not even safe.
you can create callback function for your own custom validation. Notice the comment i have did in following code.
$config=array(
array(
'field' => 'old_password',
'label' => 'oldpass',
'rules' => 'trim|required|callback_oldpassword_check' // Note: Notice added callback verifier.
),
array(
'field' => 'conf_password',
'label' => 'connewpass',
'rules' => 'trim|required|matches[password]'
),
array(
'field' => 'password',
'label' => 'newpass',
'rules' => 'trim|required'
)
In side your controller create a method as below
public function oldpassword_check($old_password){
$old_password_hash = md5($old_password);
$old_password_db_hash = $this->yourmodel->fetchPasswordHashFromDB();
if($old_password_hash != $old_password_db_hash)
{
$this->form_validation->set_message('oldpassword_check', 'Old password not match');
return FALSE;
}
return TRUE;
}
for more details of callback verification visit here
I have not verified above code. But hope you get the way to solve your problem.

Another approach:
if (!$this - > checkValidLogin($username, $old_password)) {
$this - > form_validation - > set_rules('password', 'Password', [
[
'old_password',
function($value) {
return false;
}
]
]);
$this - > form_validation - > set_message('old_password', 'Old password doesn\'t match.');
}

Please use like this, if you are using form validation library, it is working for me.
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('confirm_password', 'Confirm Password', 'required|matches[password]');
Thank You
Edit: Code formatting

Related

Set CodeIgniter error messages using Form Validation config file

Is CodeIgniter able to interpret config/form_validation.php to display appropriate error messages?
There is a way with validation when using controller: $this->form_validation->set_message('min_length', '{field} must have at least {param} characters.');
Current validation rules (and the last array is what I'm interested in, if there is a way to add validation messages in the same array as the rules.):
'users' => array(
array(
'field' => 'first_name',
'label' => 'First Name',
'rules' => 'trim|required'
),
array(
'field' => 'last_name',
'label' => 'Last Name',
'rules' => 'trim|required'
),
array(
'field' => 'email_address',
'label' => 'Email Address',
'rules' => 'trim|required|valid_email'
, 'messages'=> array('valid_email'=>'Please make sure your email is correct.')))
You Can Read And Follow The Given Example in Below Link.I Think This Will Help You.
Custom Error Messages - Form Validation Codeigniter
CodeIgniter allows to add validation messages as described in the docs. The array would be as follows:
'users' => array(
array(
'field' => 'first_name',
'label' => 'First Name',
'rules' => 'trim|required'
),
array(
'field' => 'last_name',
'label' => 'Last Name',
'rules' => 'trim|required'
),
array(
'field' => 'email_address',
'label' => 'Email Address',
'rules' => 'trim|required|valid_email'
, 'errors'=> array('valid_email'=>'Please make sure your email is correct.')))

Codeigniter Form Validation Setting Strong Password Validation Rule In an Array

I am using codeigniter form validation to validate user data when creating new user. I want to add some sort of password criteria for example password must have
at least one capital letter, a number, and one of !, #, #, $ etc) and there have to be 6-25 characters.
This is the array that I am using for validation rules:
$config = array(
array(
'field' => 'title',
'label' => 'Title',
'rules' => 'trim|required'
),
array(
'field' => 'firstname',
'label' => 'First Name',
'rules' => 'trim|required|min_length[2]|max_length[100]|xss_clean'
),
array(
'field' => 'lastname',
'label' => 'Last Name',
'rules' => 'trim|required|min_length[2]|max_length[100]|xss_clean'
),
array(
'field' => 'phone',
'label' => 'Telephone',
'rules' => 'trim|required|intiger|min_length[11]|max_length[11]|xss_clean'
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'trim|required|valid_email|is_unique[ci_user.email]|matches[conf_email]|xss_clean'
),
array(
'field' => 'conf_email',
'label' => 'Confirm Email',
'rules' => 'trim|required|valid_email|xss_clean'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|required|min_length[6]|max_length[25]|matches[conf_password]|xss_clean'
),
array(
'field' => 'conf_password',
'label' => 'Confirm Password',
'rules' => 'trim|required|xss_clean'
));
Can someone please guide me on how to achieve what I need. Thank you
you can setup call back function to check password strong validation. and call this function callback_is_password_strong in this line of your code.
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|required|min_length[6]|max_length[25]|matches[conf_password]|xss_clean|callback_is_password_strong'
),
and if you look this function will return true or false and password array rule key is validated only when it returns true
public function is_password_strong($password)
{
if (preg_match('#[0-9]#', $password) && preg_match('#[a-zA-Z]#', $password)) {
return TRUE;
}
return FALSE;
}
Using callback function
public function check_strong_password($str)
{
if (preg_match('#[0-9]#', $str) && preg_match('#[a-zA-Z]#', $str)) {
return TRUE;
}
$this->form_validation->set_message('check_strong_password', 'The password field must be contains at least one letter and one digit.');
return FALSE;
}
Usage
$this->form_validation->set_rules('adminPassword', 'password', 'required|min_length[8]|max_length[25]|callback_check_strong_password');

Form Validation required either two fields or a third one

I've searched for a while around the internet and here on StackOverflow.
I've found similar needs, but I'm having a hard time adapting those solutions to my specific need.
I'm using CodeIgniter 2, and I've created a form.
I'm using form_validation to validate each values entered in the fields.
Now, I have 2 fields which are used for a period of time, and a third field used for a fileID.
The user must either fill [the period of time] fields, or the [fileID] field, or both.
$this->form_validation->set_rules('idx_start', 'lang:idx_start', 'trim');
$this->form_validation->set_rules('idx_end', 'lang:idx_end', 'trim');
$this->form_validation->set_rules('fileID', 'lang:fileID', 'trim');
I tried to look into Callbacks and create a custom validation rule but I can't seem to figure out an easy and efficient way for my need.
Thanks in advance for your help.
You could always create different rules arrays. I do this in a couple of projects.
So something like this in your controller:
if($condition1){
$rules = $this->my_model->rules1;
} else {
$rules = $this->my_model->rules2;
}
$this->form_validation->set_rules($rules);
And where $rules1 and $rules2 in 'my_model' would look something like this:
public $rules1 = array(
'idx_start' => array(
'field' => 'idx_start',
'label' => 'start',
'rules' => 'trim|required'
),
'idx_end' => array(
'field' => 'idx_end',
'label' => 'end',
'rules' => 'trim|required'
),
'fileID' => array(
'field' => 'fileID',
'label' => 'file id',
'rules' => 'trim'
)
);
public $rules2 = array(
'idx_start' => array(
'field' => 'idx_start',
'label' => 'start',
'rules' => 'trim'
),
'idx_end' => array(
'field' => 'idx_end',
'label' => 'end',
'rules' => 'trim'
),
'fileID' => array(
'field' => 'fileID',
'label' => 'file id',
'rules' => 'trim|required'
)
);
Or however they need to be. Hope this helps!
Probably there is better way, but this could work imho:
$this->form_validation->set_rules('fileID', 'lang:fileID', 'trim')
if ( $this->form_validation->run() != true ) {
$this->form_validation->set_rules('idx_start', 'lang:idx_start', 'trim');
$this->form_validation->set_rules('idx_end', 'lang:idx_end', 'trim');
}
if ( $this->form_validation->run() == false ) {
// form failed
} else {
// form passed
}
I've used following Add in to CodeIgniter for my specific need.
http://forum.codeigniter.com/thread-22443.html
Works fine for me.
Thanks

Use language files in form_validation of Codeigniter

I´m validating a form with the codeigniter form validation class and an own form_validation file in the configuration. Additionally I´m using the language class for translation. That works perfect in view and controller but I need to get the translation also in the config/form_validation.php.
Has anybody an idea how I can do that?
Excerpt of the config/form_validation.php
$config = array(
/**
* Login form
*/
'auth/login' => array(
array(
'field' => 'email',
'label' => 'E-Mail address', <--- HERE I´D LIKE TO USE THE TRANSLATIONS
'rules' => 'trim|required|xss_clean|valid_email'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|required|xss_clean'
)
),
.....
Thanks for your ideas,
best Michael
You can use the 'lang' prefix like..
'auth/login' => array(
array(
'field' => 'email',
'label' => 'lang:email_address_label',
'rules' => 'trim|required|xss_clean|valid_email'
)

CodeIgniter - Captcha callback validation not working

I'd like some help please.
This is the array that holds all the validation on a contact form
class Contact_Form extends CI_Controller
{
private $_validation = array(
'fullname' => array(
'field' => 'fullname',
'label' => 'Fullname',
'rules' => 'trim|required|max_length[255]'
),
'email' => array(
'field' => 'email',
'label' => 'Email Address',
'rules' => 'trim|required|max_length[255]|valid_email'
),
'phone' => array(
'field' => 'phone',
'label' => 'Phone',
'rules' => 'trim|max_length[10]|integer'
),
'message' => array(
'field' => 'message',
'label' => 'Message',
'rules' => 'trim|required'
),
'captcha' => array(
'field' => 'captcha',
'label' => 'Security Code',
'rules' => 'trim|required|callback_validate_captcha'
)
);
// This is the part where I validate my contact form inside a method
$this->load->library('form_validation');
$this->form_validation->set_rules($this->_validation);
if ($this->form_validation->run() === true) {
echo 'works!';
}
This is the callback function that validates the captcha
public function callback_validate_captcha($str) {
$post_captcha = $this->input->post('captcha');
$set_captcha = $this->session->userdata('captcha');
if (strcmp($set_captcha, $post_captcha) !== 0) {
$this->form_validation->set_message('validate_captcha', '%s is wrong');
return false;
}
return true;
}
If i hit submit on an empty form I get the error that idicates that captcha is a required field, but if i submit a wrong code i don't get any error at all, which means that the callback is being ignored.
I tried to change my if statement
// change this (althought i feel its more correct)
if (strcmp($set_captcha, $post_captcha) !== 0)
// to this
if ($set_captcha != $post_captcha)
but the problem remains. Any ideas what's wrong?
you are making major mistake you have to make function validate_captcha instead of callback_validate_captcha.
Because callback is form keyword to call a function just try and bingo

Categories