Welcome Guest, Not a member yet? Register   Sign In
Central Validation
#1

[eluser]PeterPan[/eluser]
Hi,

I've got the following problem:

On my page there are several ways to add a user:
1) A user registers himself
2) Added by an administrator
3) Imported by a XML-File

Do you have any suggestions how I can get rid of redundant validationcode?
Problem is that 1) and 2) are based on a form but not 3). So I can't use the validation class?!

I can use the validation class for 3) by setting default values for all elements but $this->validation->error_string; is not resettable so for 3 users I got 1+2+3 = 6 validation strings ;(

Any suggestions?

Thanks in advance!
#2

[eluser]gtech[/eluser]
why don't you create a separate function for importing/uploading by XML and then you can parse it differently?
#3

[eluser]PeterPan[/eluser]
I have an import class where I can import for example the users via xml-upload.
What do you mean by parsing? Parse where?
#4

[eluser]tonanbarbarian[/eluser]
If you want to still use the validation class here is a suggestion
I gather that you have 2 different validation requirements, that is 2 different set of rules, and but you want to be able to manage the rules more centrally.
SO you do not want to have 1 set of rules coded in 1 or more controllers, plus the import rules coded in your import library?

If something like this is the case then what I suggest is that you put the different rules in the model. I am making a huge assumption that you are using a model for the users but it is what I would do.

So just have 2 methods in the model, formValidation and imporValidation, or just a single method with a parameter to determine which rules, then code the validation rules in the method(s).
This way the rules are all located in just a single place so that you can update and manage them easier.

Even if you need 3 seperate groups of validation rules this will work.
My suggestion is more about centrally managing the rules than anything else. And it just shows one more use for Models.
#5

[eluser]ejangi[/eluser]
I haven't personally tried this (I will in the next couple of days though) but you could use the Ruby on Rails validation method and put all your validations in the Model??? Just a thought.
#6

[eluser]PeterPan[/eluser]
Hi,

there are several issues regarding validation in models ;(
For example the error output is broken that way. But I will give that suggestion a try Wink

Thanks!
#7

[eluser]Michael Ekoka[/eluser]
Two approaches that I can suggest:

1-) Validation in the Model: be aware that the Validation class invokes the validation callback methods in the controller. To effectively bypass the controller and create your callbacks in the model, you can use a combination of the __call() magic method and the call_user_func_array(). If you are developing in php4 I'd suggest to also have a look at the overload() php function. The best approach is still to move on to php5.

Now, inside your controller you want to do something like this:
Code:
public function __call($function,$args){
        if(method_exists($this->model,$function)){    
            return call_user_func_array(array(&$this->model,$function),$args);
        }
    }
If the callback that you're calling has not been defined in the controller, it will try to call it from the model ($this->model).

Note however that the Validation::run() method, has a little routine around line 271 that checks if the callback method has been defined in the controller.
Code:
if ( ! method_exists($this->CI, $rule))
                    {        
                        continue;
                    }
That little check will effectively prevent the controller's __call() method to kick in. So, you will need to comment it out.

2-) Setting your basic validation rules in a parent controller: you can create your base controllers inside /application/libraries/MY_Controller.php. You can have as many base controller as you want. My setup usually looks like this:

Code:
// MY_Controller.php
class MY_Controller extends Controller{} // common settings for all my controllers
class Admin_Controller extends MY_Controller {} // common settings for my admin section controllers
class Application_Controller extends MY_Controller {} // common settings for front end controllers

// You can add as many base controllers here: e.g. Forms, Site, etc... I usually manage with 3
You can set your common validation rules inside these base controllers. These rules will be available to any controller that extends these parents.

In /application/controllers/
Code:
// controller_x.php
class Controller_x extends Application_Controller{}

// controller_y.php
class Controller_y extends Application_Controller{}
#8

[eluser]PeterPan[/eluser]
Thanks a lot, i'll try that!
#9

[eluser]sabya[/eluser]
@Ekoka: Awesome information.
But does it have any side effect of commenting the following code?

Code:
if ( ! method_exists($this->CI, $rule))
{        
    continue;
}
#10

[eluser]Michael Ekoka[/eluser]
If my memory serves right, it had no effect other than to negligibly speed up the process (by aborting some unnecessary steps).




Theme © iAndrew 2016 - Forum software by © MyBB