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!
#2

[eluser]hugle[/eluser]
Hello again Smile

It seems that with the version of CI from SVN:
$this->form_validation->set_rules('terms', 'Terms', 'required');
this one works,
I get an error (if unchecked the terms):
'The Terms field is required.'

Now I need to figure out, how do I change this message to custom one. Thanks for CI

cheers,
CI loving user Smile
#3

[eluser]crumpet[/eluser]
You need to use this command
$this->form_validation->set_message('rule', 'Error Message');

unfortunately it overrides all of the instances of that rule so if you used required elsewhere it will display the text you change it to.. try using isset for checkboxes (that is what the basis for the required rule is anyways) and then change the error message for isset to whatever you want.
#4

[eluser]hugle[/eluser]
[quote author="crumpet" date="1227259080"]You need to use this command
$this->form_validation->set_message('rule', 'Error Message');

unfortunately it overrides all of the instances of that rule so if you used required elsewhere it will display the text you change it to..
[/quote]
Agree. That is why I don't change it in this way. I'm gonna play with it soon.

Quote: try using isset for checkboxes (that is what the basis for the required rule is anyways) and then change the error message for isset to whatever you want.
Just tried your suggestion:
changed from:
Code:
$this->form_validation->set_rules('lookingfor', 'Looking for', 'required');
to:
Code:
$this->form_validation->set_rules('lookingfor', 'Looking for', 'isset');
and if not checked the checkbox - I don't get any error while REQUIRED gives me error
Strange ...
Any thought on this one ?

Thanks for your time crumpet
#5

[eluser]hugle[/eluser]
Hello.

as for a moment I came with one sollution on how to change an error message for one specific rule.

for example I have a rule :
Code:
$this->form_validation->set_rules('terms', 'terms', 'required');
The error I should get is: 'The terms field is required.'

So now I replace it:
Code:
$data['errors'] = str_replace('The terms field is required.', 'You must accept terms and conditions', validation_errors());

and later in the view file, I echo data['errors'] whitch contains all the validation errors and the "custom" terms error

If anyone has better sollution - please share
#6

[eluser]daweb[/eluser]
I have the same problem.

Why not, in the view file:

Code:
if( form_error('yourfile') ) echo 'Your message';
#7

[eluser]sholsinger[/eluser]
[quote author="hugle" date="1227254639"]
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:
&lt;?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;
[/quote]

I believe the documentation states that any custom validation function must require a single argument. For instance, your function would become:

Code:
&lt;?php
    function checkbox_terms($value) {
        if (empty($value)) {
            $this->form_validation->set_message('_checkbox_terms', 'You must accept Terms &amp; Conditions to proceed.');
            return FALSE;
        }
    }
?&gt;

Although, after reviewing the Form_validation class it looks like the field's rule must be: 'callback_checkbox_terms' in conjunction with the function above. Note the 'callback_' portion. That is required for callbacks to work. (Which is also stated in the User Guide)

Here's how callbacks work with CI 1.7.0:
First, Form_validation's _execute() method checks to see if the callback function exists as a method of CodeIgniter's core.
Second, it checks to see if the method exists within '$this'.
Third, if all else fails, check to see if the function is globally scoped.
Finally, if _execute() cannot find the callback function, it just ignores it.

I highly suggest you check out the User Guide if you get a chance.




Theme © iAndrew 2016 - Forum software by © MyBB