CodeIgniter Form Validation matches got too many error messages - php

I have an error message problem about form validation matches.
Here is what I set:
$this->load->library('form_validation');
$this->form_validation->set_rules('password', 'Password', 'required|trim');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'required|trim|matches[password]');
and of course, I have a form with two password input field.
form_password('password');
form_password('cpassword');
I am dealing with error messages using validation_errors() function.
if I let two password fields blank, i got:
The Password field is required.
The Confirm Password field is required.
And if I type something in password and let the Confirm Password field blank, I got:
The Confirm Password field is required.
so far so good until:
I type something in Confirm Password field and let the Password field blank, I got:
The Password field is required.
The Confirm Password field does not match the Password field.
I got two messages instead of one.
I just need the "The Password field is required." only.
What can I do for this? Please help, thanks.

In my own applications, I set up my validation rules like this
$this->form_validation->set_rules('password', 'Password', 'required|trim|matches[cpassword]');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim');
The password is required, and since it has to match cpassword then you are validating cpassword by default.

As James Lalor said above. here is the working code.
$this->load->library('form_validation');
$this->form_validation->set_rules('password', 'Password', 'required|trim');
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'required|trim');
if (!empty($this->input->post('password'))) {
$this->form_validation->set_rules('cpassword', 'Confirm Password', 'required|trim|matches[password]');
}

Related

CodeIgniter set_message not working

public function doAssignerSU() {
$this->form_validation->set_rules('username', 'Username', 'required|alpha_numeric');
$this->form_validation->set_rules('password', 'Password', 'required|alpha_numeric|min_length[4]');
$this->form_validation->set_message('min_length','Minimum of 4 characters');
When I display my validation error in my view, it still displays the preset 'The {field} must be minimum of ..'
what's my mistake? Thanks in advance
Set your custom message like this..
$this->form_validation->set_rules('password', 'Password', 'required|alpha_numeric|min_length[4]',array('min_length'=>'Minimum of 4 characters'));
Hint:You can directly set your custom messages when setting rules.
For more see Codeigniter Form Validation

CI Form Validation of Date. with input type="date"

validating a form with that has an input type="date" which takes input mm/dd/yyyy
however if i var_dump the results it comes out as yyyy-mm-dd.
1 do I validate the input type "mm/dd/yyyy", or do i validate "yyyy-mm-dd".
---im assuming I validate what the var_dump gives me which is yyyy-mm-dd.
2 More importantly I am having a hard time finding out the simplest way of going about validating date.
---I am not sure how to go about validating the date without going through by hand and checking every single date. Is there some built in functions that I could use, and how would I go about implementing that in with my CI form validations I already have.
function check_registration($post_data)
{
// Validations:
// first_name
$this->form_validation->set_rules('first_name', 'First Name', "trim|required");
// last_name
$this->form_validation->set_rules('last_name', 'Last Name', "trim|required");
// email
$this->form_validation->set_rules('email', 'Email', "trim|required|valid_email|is_unique[users.email]");
// password
$this->form_validation->set_rules('password', 'Password', "trim|required|min_length[6]");
// confirm_password
$this->form_validation->set_rules('confirm_password', 'Confirm Password', "trim|required|matches[password]");
// DOB
$this->form_validation->set_rules('DOB', 'Date of Birth', "trim|required");
// run validations:
if ($this->form_validation->run() === False)
{
// set flash data errors
$this->session->set_flashdata("reg_first_name_error", form_error('first_name'));
$this->session->set_flashdata("reg_last_name_error", form_error('last_name'));
$this->session->set_flashdata("reg_email_error", form_error('email'));
$this->session->set_flashdata("reg_password_error", form_error('password'));
$this->session->set_flashdata("reg_confirm_password_error", form_error('confirm_password'));
$this->session->set_flashdata("reg_DOB_error", form_error('DOB'));
redirect('/');
}
// No errors:
else
{
$this->insert($post_data);
redirect('/success');
}
}
Thanks in advance.
-Ants
Set your own callback rule
$this->form_validation->set_rules('DOB', 'Date of Birth', "trim|required|callback_dob_check");
Write the function inside same controller. Date validation idea taken from: Using filter_var() to verify date?.
public function dob_check($str){
if (!DateTime::createFromFormat('Y-m-d', $str)) { //yes it's YYYY-MM-DD
$this->form_validation->set_message('dob_check', 'The {field} has not a valid date format');
return FALSE;
} else {
return TRUE;
}
}
More info on DateTime:createFromFormat http://php.net/manual/en/datetime.createfromformat.php. I would stick to YYYY-MM-DD format, as it's quite often.

Email and username validation not working in yii2

I have a form with lot of fields including username and email. I also put some validation rules into the relevant model. When I submit the form I want to show the result "username already exist" if that email address is already present in the system.
I'm using:
[['email'], 'unique']
But when I submit with already existing record it doesn't show the validation error.
When I comment
[['user_name', 'full_name', 'password', 'email', 'address1', 'address2', 'city', 'state', 'country', 'zip', 'phone', 'card_number'], 'required']
rule everything works fine.
finally got it.actually their is no problem.ajax validation didn't happen for username and email .when we fill all other field and submit leaving username and email . validation work and alert us "credentials already exist "

Display single message in all validation in codeigniter

I user validation method in my codeigniter project.
In that i set rules for validation like.
$this->form_validation->set_rules('firstname', 'First Name', 'required');
$this->form_validation->set_rules('lastname', 'Last Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
Here display three difference errors when validation failed.
I want to display only one error like 'All fields are required' instead of follwing three.
You can add the attribute required in your inputs, so you don't need to send data to the server to get it checked.
<input type="text" name="fieldname" required />
and you'll get on every input a message saying that it's required.
add this line in controller
$this->form_validation->set_message('required', 'All fields are required');
$this->form_validation->set_message('valid_email', 'All fields are required'); // using this we overrideerror message found in the language file

how to set_rule to Insert in one of two fields?

If i want that out of two input field one has to be filled correctly. then only the form get submitted else it should show an error. How can we do it using set_rule function in class form_validation in codeigniter.
Let's say (for the purposes of this example) that you want to validate EITHER an email address OR a phone number, then in it's simplest form...
// If no email address, phone is required
if ( ! $this->input->post('email')) {
$this->form_validation->set_rules('phone', 'Phone Number', 'trim|numeric|min_length[11]|required');
} else {
$this->form_validation->set_rules('phone', 'Phone Number', 'trim|numeric|min_length[11]');
}
// If no phone number, email is required
if ( ! $this->input->post('phone')) {
$this->form_validation->set_rules('email', 'Email Address', 'trim|valid_email|required');
} else {
$this->form_validation->set_rules('email', 'Email Address', 'trim|valid_email');
}
...you could probably tidy it up a bit but that's the general idea of what I think you're trying to get.
Hope it helps!!

Categories