Welcome Guest, Not a member yet? Register   Sign In
My own Validation functions
#1

[eluser]reset[/eluser]
Hi all,

I've a validation helper for dates and another functions. That's the code:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

if (! function_exists('_compara_fecha_sysdate')){
    function _compara_fecha_sysdate($date){
        $fecha_actual = date('Ymd');
        $date = substr($date,6).substr($date,3,2).substr($date,0,2);
        if ($date > $fecha_actual)
            return TRUE;
        return FALSE;    
    }
}

if (! function_exists('_validar_formato_fecha')){
    function _validar_formato_fecha($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;
        return FALSE;
    }
}
    
if (! function_exists('_check_fecha')){
    function _check_fecha($date){
        if (checkdate((int) substr($date,3,2),(int) substr($date,0,2), (int) substr($date,6)))
                return TRUE;
        return FALSE;
    }
}
?>

I would like use this functions in diferents controllers, but if I use validation->set_rules(...) with that rules, it doesn't work:
Code:
$this->load->helper('date_validation');
...
$rules['date'] = 'call_back__check_fecha';
//Other option
$rules['date'] = '_check_fecha';
...
$this->validation->set_rules($rules);
...

How I can use my helper for validation with CI_Validation library?

Thanks,
#2

[eluser]xwero[/eluser]
You can create an extended validation file where you put the function is and then you can call them as regular rules
Code:
class MY_Validation extends CI_Validation
{
    function compara_fecha_sysdate($date){
        $fecha_actual = date('Ymd');
        $date = substr($date,6).substr($date,3,2).substr($date,0,2);
        if ($date > $fecha_actual)
            return TRUE;
        return FALSE;    
    }

    function validar_formato_fecha($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;
        return FALSE;
    }

    function check_fecha($date){
        if (checkdate((int) substr($date,3,2),(int) substr($date,0,2), (int) substr($date,6)))
                return TRUE;
        return FALSE;
    }
}
// usage
$field['date'] = 'compara_fecha_sysdate';
You will have add messages for those rules in the validation language file too.
#3

[eluser]reset[/eluser]
Thanks xwero, in my new class (MY_Validation), How do I add the library of codeigniter (CI_Validation)?

*Making include the full path (system \ libraries \ Validation.php)
*Loading from the controller
*Loading from MY_Validation

Thanks
#4

[eluser]reset[/eluser]
Sorry, I'm reading in the user_guide. Thanks




Theme © iAndrew 2016 - Forum software by © MyBB