2

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

2
  • Maybe it's just a typo here, but you wrote "accept_tearms" instead of "accept_terms" in your rules Commented May 31, 2013 at 9:40
  • ahh. my mistake.. It's not typo, I've updated my question Commented May 31, 2013 at 9:42

2 Answers 2

10

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;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. But Is there any easy solution to using with form_error function.
@Mifas - another method added :)
I didn't realize we can use $_POST variable with in our callback function. Thanks. Its working perfectly
You may want to rename the accept_terms() function _accept_terms() and callback_accept_terms to callback__accept_terms, other wise the accept_terms method will be accessible via the browser @ sample.com/controler/accept_terms any function that starts with a underscore is hidden from public access.
0

you can put the value=1 and check with codeigniter like this:

<input type="checkbox" name="accept_terms" value="1" /> Accept TOS<br>
$this->form_validation->set_rules('accept_terms','TOS','trim|required|xss_clean|greater_than[0]');

]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.