I've a form with checkbox for accepting TOS. The problem is I need to add custom error for this checkbox,
<form>
<input type="text" name="fname" placeholder="Name" /><?php echo form_error('fname') ?>
<input type="text" name="email" placeholder="Email" /><?php echo form_error('email') ?>
<input type="text" name="password" placeholder="Password" /><?php echo form_error('password') ?>
<input type="checkbox" name="accept_terms" value="yes" /> Accept TOS<br>
<?php echo form_error('accept_terms') ?>
</form>
PHP
<?php
$this->form_validation->set_rules('fname','First Name','trim|required|xss_clean');
$this->form_validation->set_rules('email','Email','trim|required|xss_clean|valid_email');
$this->form_validation->set_rules('password','Password','trim|required|xss_clean');
$this->form_validation->set_rules('accept_terms','TOS','trim|required|xss_clean'); // Need to add custom error message
if ( $this->form_validation->run() === TRUE ) {
}else{
}
?>
When the user does not select TOS, Then I have to say
Please read and accept our terms and conditions.
Note: I've added form_error function to display individual errors
I will do something like this,
if ($this->form_validation->run() === TRUE ) {
if(!$this->input->post('accept_terms')){
echo "Please read and accept our terms and conditions.";
// Redirect
}
}
else{
}
And for custom message you can call a custom validate function like,
$this->form_validation->set_rules('accept_terms', '...', 'callback_accept_terms');
And then set up this method in the controller:
function accept_terms() {
if (isset($_POST['accept_terms'])) return true;
$this->form_validation->set_message('accept_terms', 'Please read and accept our terms and conditions.');
return false;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With