Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make CodeIgniter callable work for checkboxes that are left empty, custom form validation bug

I have a problem with my form validation rules (in a seperate config file). This occurs with checkboxes.

To organize my validation rules, I created a library file called validation_rules. This library contains all my custom callbacks like valid_date etc. To be able to call these rules, I load the library, and then use the following config:

array(
    'field' => 'terms',
    'label' => 'lang:reg_lbl_terms',
    'rules' => array(array('terms_accepted', array($ci->validation_rules, 'terms_accepted')))
    ),

Where $ci is a reference to CodeIgniter ($this).

Now this works fine for most types of input, but it doesn't work for checkboxes that are left empty, probably since they don't get posted.

HOWEVER, when I ditch my library and simply add the callback to the controller, everything works fine with the following config:

array(
    'field' => 'terms',
    'label' => 'lang:reg_lbl_terms',
    'rules' => array('callback_terms_accepted')
    ),

Also, when I add the rules required anywhere in the rules array (or string), the required rule does get called (returning false since the checkbox is not checked), but all other rules are completely ignored.

This must be a bug in CodeIgniter right? Does anybody have a solution or workaround? Of course this is an option, but I really don't like it.

Relevant documentation: http://www.codeigniter.com/user_guide/libraries/form_validation.html

Edit: Checkbox PHP/HTML:

<?php
$data = array(
    'name'    => 'terms',
    'value'   => 'true'
);
echo form_checkbox($data);
// Results in: <input type="checkbox" name="terms" value="true">
// No set_value() is used.
?>
like image 435
Wouter Florijn Avatar asked Sep 08 '15 12:09

Wouter Florijn


1 Answers

You can create your rules in a form_validation file inside /config

application/config/form_validation.php

$config = array(
    'controller/method' => array(
        array('field'=>'', 'label'=>'', 'rules'=>'required|acceptTerms')
        array('field'=>'another', 'label'=>'', 'rules'=>'required')
    ),
);

Note the controller/method for the key, Codeigniter will use this if you don't set it specifically inside the calling form_validation function.

An example of this would be like so

application/controllers/Shop

class Shop extends CI_Controller
{
    public function __construct(){ parent::__construct(); }

    public function index()
    {
        // Show the Purchase Form
        // in some view
        return $this->load->view();
    }

    public function purchase()
    {
        // notice we don't pass any params to the run() function
        // codeigniter will look inside application/config/form_validation/$config
        // for any matching key, ie: shop/purchase

        if(!$this->form_validation->run()){ 
            // If validation fails, show the form again
            // and stop the method from executing any further
            return $this->index();
        }
    }
}

To validate if check boxes are set, we look for the keyword on

application/libraries/MY_Form_validation.php

class MY_Form_validation extends CI_Form_validation
{
    public function __construct($config)
    {
        parent::__construct( $config );
    }

    public function acceptTerms( $field )
    {
        $this->set_error('acceptTerms', 'your error message');

        return (bool) $field == 'on';
    }
}
like image 97
Philip Avatar answered Nov 14 '22 23:11

Philip