CodeIgniter form validation rules for checkbox CodeIgniter form validation rules for checkbox codeigniter codeigniter

CodeIgniter form validation rules for checkbox


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;}


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