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!!
Related
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]');
}
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.
Well here is a example error Codeigniter will shoot out at the user if they don't type in their email.
email: The Email field is required.
Now the "email" part specifically. How can I change it to whatever I like? It seems its using the inputs name/id.
Example of how I setup form validation for email.
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[user.email]');
So maybe a better example .. say I wanted to change it from saying "email" to "The Email"
Thanks in advnace.
You should use set_message function in form_validation library.For example;
$this->form_validation->set_message('email', '%s is entered email adress, it's wrong!');
CodeIgniter's user-guide is very easy and interactive.You should check the documentation for these type of basic questions.
CodeIgniter user-guide
This is how I do it:
If you want to alter the message use set_message:
$this->form_validation->set_message('required', 'Your custom message here');
Using a Callback is a better way to do it:
$this->form_validation->set_rules('username', 'Username', 'check_user_name');
public function username_check($str)
{
if (strlen($str)<0)
{
$this->form_validation->set_message('check_user_name', 'The %s field is empty"');
return FALSE;
}
else
{
return TRUE;
}
}
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
I have a callback function that check_captcha which sees if $row is ==0 or == 1 (this information is queried from sql).
The problem is that I can not call it from $self->form_validation->set_rule('captcha', 'call_back_check_captcha') due to the fact that my function takes in a $row var. The way I'm calling it now I get a Unable to access error message. How can I make this work?
function check_captcha( $row)
{
if($row ==0)//didnt find any
{
$this->form_validation->set_message('captcha', 'text dont match captcha');
return FALSE;
}
else
{
return TRUE;
}
}
function create_member()
{
$past = time() - 7200;
$this->db->query("DELETE FROM captcha WHERE captcha_time <".$past);
$sql = "SELECT COUNT(*) AS count FROM captcha WHERE word =? AND ip_address =?";
$binds = array($_POST['captcha'], $this->input->ip_address(), $past);
$query= $this->db->query($sql, $binds);
$row = $query->row(); //row query rows : if it found an entry =1
$self->check_captcha($row->count);
//VALIDATIONS
$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_address', 'Email Address', 'trim|required|valid_email|unique[user.email_address]');
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|unique[user.username]');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_leng[32]');
$this->form_validation->set_rules('password2', 'Password Confirmation','trim|required|matches[password]');
if(!$_POST['captcha']){
$this->form_validation->set_rules('captcha', 'Captcha','trim|required');}else{
$this->form_validation->set_rules('captcha', 'Captcha', 'callback_check_captcha');}
if($this->form_validation->run()==FALSE)
{ //this -> to the curr obj(UserController) && registraion() points to the the function in controller
$this->registration(); //reloads reg page so they can fill out right stuff
}
else
$this->form_validation->set_message('check_captcha', 'text dont match captcha');
The message name corresponds to the function, not the field. So setting it to "check_captcha" will fix your bug. The error message will use the correct field name.
Actually the best way, instead of write the error message directly on controller, would be add this entry "check_captcha" on languages.
In my case, the message for validation rule (form validation) "less_than" was not present.
I changed the file /system/language/??/form_validation_lang.php. I've added the missing entry.
That helped me
go to application/config/autoload.php and add "Security" helper class there.
$autoload['helper'] = array('security');
Or add this before your form validation
$this->load->helper('security');
You can set error message in set_rules :
$this->form_validation->set_rules('captcha', 'Captcha', 'callback_check_captcha',
array('check_captcha' => 'text dont match captcha'));
add a entry to your language file with named of the part inside the (yourfieldname) of the errormessage - thats solved the problem
Even if the question is already answered, there is another error that can lead to the same error message:
If you call your callback checkCaptcha, it will not work. Prefer always a name like check_captcha as recommended in Codeigniter/General Topics/PHP style guide.