CodeIgniter Forums
Validation should work with Array and Objects? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: Validation should work with Array and Objects? (/showthread.php?tid=5505)



Validation should work with Array and Objects? - El Forum - 01-24-2008

[eluser]Newton Wagner[/eluser]
Hi there again,

Talking about MVC and Validation class with others, I've noted that Validation class should only run at the Controller, because it uses the $_POST array.

In my opinion, validation is a business logic, and should run in a Model class.


What do you think about Validation class works with an Array passed thru the run() method, or something like set_validation_data(Array $data)?

If you do not set anything, by default Validation set this array with the $_POST var. So, we maintain the compatibility.


Sounds good?


Validation should work with Array and Objects? - El Forum - 01-24-2008

[eluser]Seppo[/eluser]
I donĀ“t get why you say it only runs at the controller... the $_POST array is superglobal and can be accesed any place.


Validation should work with Array and Objects? - El Forum - 01-24-2008

[eluser]Newton Wagner[/eluser]
You are right Seppo, it works.

But it's not Model duty to work with Request or Response data, it's job for the Controllers.


I'm not saing that this do not work, I just want to work with MVC properly. Smile.


Validation should work with Array and Objects? - El Forum - 01-24-2008

[eluser]wiredesignz[/eluser]
I also have issue with Validation callback functions not working in Models ie: checking if a username is unique during user registration. It should be able to ask the Users_model directly.


Validation should work with Array and Objects? - El Forum - 01-24-2008

[eluser]wiredesignz[/eluser]
I have created a MY_validation library which allows callbacks in Models.

Save the existing Validation library as MY_validation with only the original run() function in it.
Alter the class identifier to MY_ validation and extend it off CI_Validation.

Change this code appropriatley:

Code:
// Call the function that corresponds to the rule
    if ($callback === TRUE)
    {                    
        if (strpos($rule, '->'))
        {
            list($class, $method) = split('->', $rule);
            
            if ( ! method_exists($this->CI->$class, $method))
            {        
                continue;
            }
        
            $result = $this->CI->$class->$method($_POST[$field], $param);
        }
        else
        {
            if ( ! method_exists($this->CI, $rule))
            {        
                continue;
            }
            
            $result = $this->CI->$rule($_POST[$field], $param);    
        }
        
        // If the field isn't required and we just processed a callback we'll move on...
        if ( ! in_array('required', $ex, TRUE) AND $result !== FALSE)
        {
            continue 2;
        }
    }
    else ....

my callback declaration then becomes 'trim|required|callback_users->is_unique[username]';


Validation should work with Array and Objects? - El Forum - 01-24-2008

[eluser]wiredesignz[/eluser]
Also posted in Ignited Code