Welcome Guest, Not a member yet? Register   Sign In
Validation callback function inside model
#1

[eluser]Base Willy[/eluser]
Hi!

I've found that validation callback functions doesn't work if the function is declared inside the model, not inside the controller. Is it possible to make them work that way? I thought storing those functions in model would be better and cleaner
#2

[eluser]gtech[/eluser]
Not sure I agree with moving the validation to the model.. I use the models for processing data for database manipulation, managing rollbacks, error logging etc. I would suggest that the controller should be dealing with user interface interactions (parsing url information, building variables to pass to a view, form validation etc)

extract from documentation:

-----------------------
What is a Model?

Models are PHP classes that are designed to work with information in your database.
-----------------------

if your function is getting messy you can extract code to libraries (for reuse) OR create an _ function in the controller ( _ means you cannot load it from the browser)

e.g.

Code:
..
/****
*/
fucntion external() {
  $data=$this->_internal();
  echo $data;
}
/****
*/
fucntion _internal() {
  return "I like to teach the world to sing";
}
#3

[eluser]Base Willy[/eluser]
I understand that models should work with database. But my calback function checks if entered email exists in the database so i think model is the right place for it. Moreover, this function is used only by Model's method, and I think it is a pretty good reason to store it in the Model instead of Controller.
#4

[eluser]gtech[/eluser]
sorry I assumed if you were using the validation library you would be doing it for form data.. (i assumed thats what its for). which library are you using?

if you are using the validation library:

if you look at system\libraries\validation.php the callbacks rely on POST data. I guess you could build up some postdata in the model (yuck!). there are also some external functions you can call to bypass the call back, still not sure I recommend it though.

validation.php
Code:
function valid_email($str)
    {
        return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $str)) ? FALSE : TRUE;
    }
#5

[eluser]Base Willy[/eluser]
I'm using the standard validation.php and all proccess of validating the form and adding info to the database is stored in model, i didn't want to have validation inside controller.. My controller just calls Model's method register() which returns false if something went wrong and registration didn't go well and true if everything's OK. Then the controller desides what to show - the form again or success message.
#6

[eluser]gtech[/eluser]
Quote:I’m using the standard validation.php and all proccess of validating the form and adding info to the database is stored in model, i didn’t want to have validation inside controller.. My controller just calls Model’s method register() which returns false if something went wrong and registration didn’t go well and true if everything’s OK. Then the controller desides what to show - the form again or success message.

After reading the validation code, I think the validation library works on the assumption you validate the form BEFORE you pass the data to a model (it tends to be standard practice). I am not convinced it will work the way you want it to .. sorry.., you could try passing the POST data down to the model.

[edit] Use the callback code in post below, it will achieve what you want.
#7

[eluser]gtech[/eluser]
there is a section in the documentation, and I think it is how you should approach your design, if you want the validation to work with out any probs.

Callbacks: Your own Validation Functions

Code:
//COPIED FROM CODEIGNITER DOCS
    function index()
    {
        $this->load->helper(array('form', 'url'));
        
        $this->load->library('validation');
            
        $rules['email']        = "callback_email_check";
        // might be able to do
        // $rules['email']        = "required|callback_email_check";
        
        $this->validation->set_rules($rules);
            
        if ($this->validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
            $this->load->view('formsuccess');
        }
    }
    
    function email_check($str)
    {

             // CALL MODEL HERE !! TO check EMAIL exists in DB
             // THEN RETURN TRUE OR FALSE
    }
#8

[eluser]Base Willy[/eluser]
gtech, my design is just like that, only it's in the model, not in the controllerSmile
#9

[eluser]gtech[/eluser]
maybee it shouldnt be in the model! ... lol ... to save us going around in circles we might have to agree to disagree :-) .

lets assume your way is the correct way to do it, from reading the code you have to check the following:


are you passing the $_POST data to the model?...

check its there by echoing $_POST in your model just before you call validation->run(). if its not there it wont work!

are you attempting to load the view in the controller?...

if so I assume you parsed out $this->validation->error_string in the model, as it wont be available in the view unless you return a reference to it.


hope that helps.... good luck!
#10

[eluser]Base Willy[/eluser]
gtech, thanks for your help. $_POST data is received by the model and basic validation works just well. The only problem is that if callbac func declared inside the model, it desn't work. I moved it to the controller and everythig's fine that way, validation and callback works. So validation code is in the Model but callbacks are in the Controller.
Sorry if i don't get something, i'm pretty new to MVC and CI :-[




Theme © iAndrew 2016 - Forum software by © MyBB