1
votes

I've got a problem using the form_validation library with CodeIgniter 3.x :

I want to use a model function as a validation rule for an input. When I submit my form, it seems that the function isn't called at all. According to the user guide, here's the way I do it from my controller :

if($this->input->post() !== false) 
    {
        $this->form_validation->set_rules(
            'username', //the post value to check
            'User Name',
            array(
                'trim',
                'required',
                array($this->user_model, 'checkUserName') // i want to call checkUserName from the user_model model
            )
        );

I don't understand why it doesn't even calls the checkUserName function in the user_model : does anyone has an idea ?

[EDIT] : I saw the source code for the set_rules() function in system/libraries/form_validation.php. It seems that if the third parameter is not a string, it does nothing, so I cannot call a model function. userguide is not uptodate ? Here's the lines of code that I found from form_validation :

// No fields? Nothing to do...
    if ( ! is_string($field) OR  ! is_string($rules) OR $field == '')
    {
        return $this;
    }
2
Have you load model file in your constructor and make sure you are running PHP 5.3+ !!Saty
Yes : I'm running php 5.6.10 and the model is loaded in the constructor of my controller.Rukien
Have you got any error??Saty
Log threshold is defined to 4 in my config.php file : nothing suspect, even in th php error log. It seems the form_validation just ignores this ruleRukien
Have you code enter into this condition if($this->input->post() !== false) Saty

2 Answers

0
votes

if you are checking username for not to duplicate used the builtin function is_unique['table.field].
table:name of table.
field: is name of field that is going to be check.

$this->form_validation->set_rules('username', User Name, 'is_unique['users.username]');

this will check dbtable users for field Username.if the provided username is find it will return error username not unique.

this is is_unique() function defined in form_validation class :

public function is_unique($str, $field)
    {
        sscanf($field, '%[^.].%[^.]', $table, $field);
        return isset($this->CI->db)
            ? ($this->CI->db->limit(1)->get_where($table, array($field => $str))->num_rows() === 0)
            : FALSE;
    }

if you really want to call your own custom function then ...
the best is to extend the form_validation class:as follow.

class MY_Form_validation extends CI_Form_validation {
    protected $CI;
    public function __construct()
    {
        parent::__construct();
        $this->CI =& get_instance();
    }

    public function check_user_name($str)
    {
      //here you codes goes eg.$this->CI->db->get_where(); etc.
    }
}
0
votes
'premium_dates' => [
        'field'  => 'premium_dates',
        'label'  => 'Premium dates',
        'rules'  => [
            'trim',
            ['validate_premium_dates', function () {
                return $this->Properties_seasons_model->validate_premium_dates();
            }],
        ],
        'errors' => [
            'validate_premium_dates' => 'Premium dates enabled. Please choose at least one Premium Date.',
        ],
    ];