Welcome Guest, Not a member yet? Register   Sign In
Validation should work with Array and Objects?
#1

[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?
#2

[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.
#3

[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.
#4

[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.
#5

[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]';
#6

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




Theme © iAndrew 2016 - Forum software by © MyBB