Display single message in all validation in codeigniter - php

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

Related

CodeIgniter Form Validation matches got too many error messages

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]');
}

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.

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!!

Codeigniter stripping out pound signs (£) using form validation

I've used Codeigniter for a while and whilst I've not always found the form validation as straightforward as I'm sure it should be I've never had any massive problems... until now!
I have a simple form comprised of text inputs and a textarea. The form starts off prepopulated and, if the validation fails, repopulates it with the last changed state.
My problem is this - The textarea needs to accept pound signs (£). It populates text from the database absolutely fine but on submit, whether the form validates or not, it strips them out, regardless of what I do!!
I've scoured the net and can only find solutions about applying things like htmlentities to the validation rules, but if I firephp the post data out, even before the rules, it's already been stripped out.
global_xss_filtering is set to false in my config.
It's driving me mad and wasting way more time than it should... has anyone got a solution to this, I know I'm probably missing something really simple - it's maddening!
Thanks,
Helen
Here's my validation code, although the firephp log at the top shows it to already be stripped out so I can't see how doing anything here will help... I've tried adding the various php function as it suggests HERE (codeigniter manual) but it makes no difference at all.
public function edit_entry2($entry_id, $page_id) {
$this->firephp->log($_POST);
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Name', 'required|max_length[255]');
$this->form_validation->set_rules('address1', 'Address line 1', 'max_length[255]');
$this->form_validation->set_rules('address2', 'Address line 2', 'max_length[255]');
$this->form_validation->set_rules('address3', 'Address line 3', 'max_length[255]');
$this->form_validation->set_rules('address4', 'Address line 4', 'max_length[255]');
$this->form_validation->set_rules('county', 'County', 'required|max_length[255]');
$this->form_validation->set_rules('post_code', 'Post Code', 'max_length[10]');
$this->form_validation->set_rules('telephone1', 'Telephone 1', 'required|max_length[12]|is_natural');
$this->form_validation->set_rules('telephone2', 'Telephone 2', 'max_length[12]|is_natural');
$this->form_validation->set_rules('fax', 'Fax', 'max_length[12]|is_natural');
$this->form_validation->set_rules('email', 'Email address', 'valid_email');
$this->form_validation->set_rules('website', 'Website', 'max_length[255]');
$this->form_validation->set_rules('rating_awards', 'Rating/Awards', 'max_length[255]');
$this->form_validation->set_rules('description', 'Description', 'max_length[1000]');
$this->form_validation->set_rules('categories[]', 'Categories', 'callback_categories_check');
if ($this->form_validation->run() == FALSE)
{
$this->edit_entry($entry_id, $page_id);
}
else
{
$updated_entry = array('name'=>$_POST['name'], 'address1'=>$_POST['address1'], 'address2'=>$_POST['address2'], 'address3'=>$_POST['address3'], 'address4'=>$_POST['address4'], 'county'=>$_POST['county'], 'post_code'=>$_POST['post_code'], 'telephone1'=>$_POST['telephone1'], 'telephone2'=>$_POST['telephone2'], 'fax'=>$_POST['fax'], 'email'=>$_POST['email'], 'website'=>$_POST['website'], 'rating_awards'=>$_POST['rating_awards'], 'description'=>$_POST['description']);
$this->tourism_catalogue_model->update_entry($entry_id, $updated_entry, $_POST['categories']);
$this->index($page_id);
}
}
You can use validation callbacks. So, when your validation rule is triggered, it will then fire a callback function that you could then use to strip out or inject the pound symbols as desired.
Form Validation Callbacks

Categories