views:

16

answers:

1

Hi,

I'm using the technique of saving codeigniter form validation rules to a config file as specified here, but can't seem to get it working.

I am also attempting to invoke validation callback functions from a Validation_Callbacks.php library which I'm autoloading via the autoload.php mechanism.

Below is a snippet from my form_validation.php form validation rules config file:

<?php
/* This config file contains the form validation sets used by the application */

$config = array(
    'register_user' => array(
            array(
                    'field' => 'dob',
                    'label' => 'lang:register_dob',
                    'rules' => 'trim|required|date_check|age_check|xss_clean'
                ), ...
            )
          )

And here is the Validation_Callbacks.php file, that lives under application/libraries:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
/**
* Custom validator library containing app-specific validation functions
*/
class Validation_Callbacks {
/* Checks that the given date string is formatted as expected */
function date_check($date) {
    $ddmmyyyy='(0[1-9]|[12][0-9]|3[01])[- \/.](0[1-9]|1[012])[- \/.](19|20)[0-9]{2}';
    if(preg_match("/$ddmmyyyy$/", $date)) {
        return TRUE;
    } else {
        $this->form_validation->set_message('date_check', $this->lang->line('error_register_dob_format'));
        return FALSE;
    }
}

/* Checks that the given birthday belongs to someone older than 18 years old. We assume
* that the date_check method has already been run, and that the given date is in the
* expected format of dd/mm/yyyy */
function age_check($date) {
    $piecesDate = explode('/', $date);
    $piecesNow = array(date("d"), date("m"), date("Y"));
    $jdDate = gregoriantojd($piecesDate[1], $piecesDate[0], $piecesDate[2]);
    $jdNow = gregoriantojd($piecesNow[1], $piecesNow[0], $piecesNow[2]);

    $dayDiff = $jdNow - $jdDate;

    if ($dayDiff >= 6570) {
        return TRUE;
    } else {
        $this->form_validation->set_message('age_check', $this->lang->line('error_register_age_check'));
        return FALSE;
    }
}

}

I'm invoking this using the standard:

if ($this->form_validation->run('register_user') == FALSE) {

My callback functions are not being called. Is there something I'm missing here? Thanks in advance for any help you may be able to provide!

+1  A: 

Ensure that:

When a rule group is named identically to a controller class/function it will be used automatically when the run() function is invoked from that class/function.

To test you can try using:

$this->load->config('configname');
Kieran Andrews
Thanks Kieran, I was giving them arbitrary names and then trying to use $this->form_validation->run('name'). This is supposed to work according to the documentation, but your solution is better - it just stops me from farming off the validation call to a delegate method and forces me to include it in the top-level 'controller/function' function.
ubermensch