Welcome Guest, Not a member yet? Register   Sign In
[SOLVED] Form validation callback method in MODEL instead of controller -- Read me if you want the answer.
#1

[eluser]Mirge[/eluser]
So in my model I have:

Code:
$this->form_validation->set_rules('how_completed', 'How is the transaction completed?', 'callback_how_completed');

...
...
...

    public function how_completed($notUsed)
    {
        echo "got here - outside if statement"; exit;
    }

And the callback is NOT being called. Thoughts?
#2

[eluser]Mirge[/eluser]
And apparently it doesn't want to give me a log file. /sigh.

EDIT: fixed log file issue, but don't see anything in log that'd help.
#3

[eluser]Mirge[/eluser]
I've also tried creating an isolated test... this also is NOT calling my callback method. Any idea why?

Code:
<?php

class MTest extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
    }
    
    public function save_data()
    {
        echo "inside save_data() - before<br/>";
        $this->form_validation->set_rules('some_field', 'Some Field', 'callback_fakefield');
        $this->form_validation->run();
        echo "inside save_data() - after<br/>";
    }
    
    public function fakefield()
    {
        echo "got here inside fake field callback function.";
        return false;
    }
}

Here's the output:

Code:
before
inside save_data() - before
inside save_data() - after
after
#4

[eluser]Mirge[/eluser]
edited, maybe it can't find the callback in the model, it has to be in controller?
#5

[eluser]Mirge[/eluser]
Sigh. SO... you can't have form validation with callbacks in a model in CI by default? Unreal... unless I'm just missing something.

EDIT:

Ran into this: http://codeigniter.com/wiki/MY_Form_vali...to_Models/

But it also did not work. Out of time, I just put the callback methods in the controller and called it good for now, but would REALLY love for somebody to chime in with a working solution so I can move the callback methods back into the model. Thanks!
#6

[eluser]Mirge[/eluser]
Not able to find a working MY_Form_validation class that'll allow callbacks in model.

Anybody know of a working one?
#7

[eluser]Mirge[/eluser]
After MUCH headache... I decided to revisit the previous URL and re-try the code.

It still didn't work.

I started tracing through it... and the example shows you're supposed to invoke a callback method in the MODEL via:


callback_methodname_model[modelname]


So, I continued tracing through the code... echo statements & what not. Then I discovered that $rule was NOT saving the [modelname] portion.

Seconds later, I found the regular expression:

Code:
if (preg_match("/(callback_\w+)/", implode(' ', $rules), $match))

needless to say, I let out a sigh of relief. \w won't match brackets.

I changed the regex to:

Code:
if(preg_match("/(callback_[\w\[\]]+)/", implode(' ', $rules), $match))

And it's now calling the correct validation method within the model specified. All is right with the world again.

Figured I'd share... since the only forum results I could find via searching were not helpful. Hopefully this saves somebody else the hassle/headache.




Theme © iAndrew 2016 - Forum software by © MyBB