Welcome Guest, Not a member yet? Register   Sign In
Form_validation - checking checkboxes problem. Any help appreciated! (CI 1.7)
#1

[eluser]hugle[/eluser]
Hello everyone.

I've been playing for a long time with form validation.
But came with problems while checking checkboxes and can't handle them myself.

So i wrote my own 'rule':

In controller I have:
Code:
$this->form_validation->set_rules('terms', 'Terms', 'checkbox_terms');

And in application/libraries/MY_Form_validation.php:
Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_Validation {

    var $_error_prefix        = "<div class='error'>";
    var $_error_suffix        = '</div>';
    
    function MY_Form_Validation( $rules = array() ) {
        parent::CI_Form_Validation();
        $this->_config_rules = $rules;
        log_message('debug', "My Form Validation Class Initialized");
    }


    function checkbox_terms() {
        if ($_POST) {
            if (array_key_exists('terms', $_POST)) {  
                return TRUE;
            } else {
                $this->form_validation->set_message('_checkbox_terms', 'You must accept Terms &amp; Conditions to proceed.');
                return FALSE;
            }
        }    
    }


} ?&gt;

The problem is that this won't work at all.


I tried to do a callback:
$this->form_validation->set_rules('terms', 'Terms', 'callback_checkbox_terms');

and wrote a function in the same controller:
Code:
function checkbox_terms() {
        if ($_POST) {
                if (array_key_exists('terms', $_POST)) {  
                    return TRUE;
                } else {
                    $this->form_validation->set_message('_checkbox_terms', 'You must accept Terms to proceed');
                    return FALSE;
                }
            }    
    }

and it WORKS!

What I'm doing wrong with Extendind Form_validation ?
I find it more comfortable to use "extensions"

Thank you everyone!


Messages In This Thread
Form_validation - checking checkboxes problem. Any help appreciated! (CI 1.7) - by El Forum - 11-20-2008, 08:03 PM



Theme © iAndrew 2016 - Forum software by © MyBB