Hi guys,
My first post! so be nice!
The issue I am having is with extending the CI_Form_Validation class. I have a few extra validation methods that i need to integrate into my form validation, so thought, rather than using callbacks, i would try and the extend the class….
Now, i believe i have extended the class fine, but when calling any function on the class from my form validation array, it just doesn’t work. It doesn’t pick up the method at all.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Formvalidation extends CI_Form_Validation {
var $CI;
/**
* Contructor
* Extends core Form Validation class. Sets CodeIgniter super object.
* @param array $config
* @author Craig Barber
* @version 1.0
*/
function __construct()
{
parent::CI_Form_Validation();
$this->CI =& get_instance();
}
/**
* Valid date
* Checks if input is a valid date.
* @param string $str
* @return bool
* @author Craig Barber
* @version 1.0
*/
function valid_date($str)
{
if (preg_match('/([0-3][0-9])\/([0-9]{1,2})\/([1-2][0-9]{3})/', $str, $date)):
return checkdate($date[2], $date[1], $date[3]);
else:
return false;
endif;
}
/**
* Valid date
* Checks if input matches a value.
* @param string $str, string $val
* @return bool
* @author Craig Barber
* @version 1.0
*/
function same_as($str, $val)
{
if ($str === $this->CI->input->post($val)) return true;
else return false;
}
/**
* Check num of entries
* Checks if input matches a value.
* @param string $str, string $val
* @return bool
* @author Craig Barber
* @version 1.0
*/
function check_num_entries($str){
if($str >= 4){
return true;
}else return false;
}
function checkPayment($str, $val){
if($this->CI->input->post($val) == 'regular_payments'){
//now we need to check whether the payment frequency is set when making regular payments
if($str == '0'){
//then return false if payment frequency is left blank
return false;
}else{
return true;
}
}
}
?>
Now, I have my form validation in a config array, which sets the validation rules for the whole entire site. This array is stored on a php page named form_validation.php which is held in my config folder. Below is the section for the page which I am trying to write the extra form function for, all the functionality works apart from where i am trying to call my added function named checkPayment.
'payment/create' => array(
array(
'field' => 'nbx_payment_amount',
'label' => 'Payment amount',
'rules' => 'required|numeric'),
array(
'field' => 'nbx_payment_type',
'label' => 'Payment type',
'rules' => 'required'),
array(
'field' => 'nbx_payment_date',
'label' => 'Payment date',
'rules' => 'required|valid_date'),
array(
'field' => 'nbx_payment_number',
'label' => 'Number of payments',
'rules' => 'required|numeric'),
array(
'field' => 'nbx_payment_frequency',
'label' => 'Payment frequency',
'rules' => 'checkPayment[nbx_payment_type]')),
I have tried adding the extended library directly in my controller as seen below:
$this->load->library('MY_Formvalidation');
But this doesn’t seem to do anything either, I have also added the library to my autoload file to automatically load the library, no joy there either.
My first question really, is an you extend the CI Form Validation library, and call those extra functions from a validation array? All the examples I have seen on the web do not show this functionality working with a validation array, but setting up the rule in the controller itself (which I have also tried)
Does anyone know of the best way to get around this problem? I’m even residing to the fact that I might have to use callbacks, which is fine, but I couldn’t seem to get that working either!!!
It’s got to the point where I just want a fix….any help welcome!
thanks guys
Craig
