Welcome Guest, Not a member yet? Register   Sign In
Validation callback in a CI package
#1

[eluser]Johnny Freeman[/eluser]
I cannot, for the life of me, get form validation callbacks to work in a CI package (application/third_party). Here is a thread I created on MojoMotor's forum (with no luck) explaining the issue and with code examples.

Are you supposed to be able to use callbacks in a package?
#2

[eluser]Johnny Freeman[/eluser]
Come on, someone has got to know the answer to this.
#3

[eluser]InsiteFX[/eluser]
You need to specify what version of CI you are using!

But as far as I know about From_Validations the callback has to be
in the same class.

InsiteFX
#4

[eluser]Johnny Freeman[/eluser]
I'm using CI 2 core (not reactor). It comes with the MojoMotor CMS. I'm making an addon for MM which is essentially a CI package.

Are you suggesting that some version of CI does support the use of callback validations from within packages?

FYI, my callback is in the same class.
#5

[eluser]Johnny Freeman[/eluser]
Code:
class Form
{

    /**
     * CONSTRUCTOR
     */
    function __construct()
    {
        $this->ci =& get_instance();
        
        session_start();
    }


    /**
     * form gets submitted here
     */
    function process($data)
    {
        // CAPTCHA
        if (isset($_POST['captcha_response']))
        {
            $this->ci->form_validation->set_rules('captcha_response', 'CAPTCHA', 'callback_check_captcha');
        }

        $this->ci->session->set_flashdata('errors', validation_errors('<li>', '</li>'));
    }


    /**
     * validation callback, this is obviously not a real captcha test
     */
    function check_captcha($str)
    {
        if ($str == 'test')
        {
            $this->ci->form_validation->set_message('check_captcha', 'The %s field can not be the word "test"');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
}

This example should work... but it does not.

I've also tried extending the form_validation library from within a MM addon (or CI package) which doesn't seem possible either.
#6

[eluser]kenny.katzgrau[/eluser]
Is the code snippet above a controller being called directly?

In the core library, callbacks are called on the CodeIgniter Superobject (ie, the controller).
#7

[eluser]kenny.katzgrau[/eluser]
Actually, I realized that you're doing this from inside a package.

Because you can't use controllers inside packages (they are essentially a bundle of libraries), you won't be able to use callbacks.

Just to say that a little more clearly, callbacks are only called on controllers, and controllers won't be loaded from within packages.
#8

[eluser]Johnny Freeman[/eluser]
Thank you VERY much! That's exactly what I was looking for. Now my question is, would it be possible for me to somehow "inject" the callback into the CI Superobject? Forgive me, that's probably a stupid question.

OR

Is it possible to extend core libraries from within a package?
#9

[eluser]Johnny Freeman[/eluser]
It's okay if it's not possible because I can do what I want using raw php but I'd rather use some CI goodness if I can.
#10

[eluser]kenny.katzgrau[/eluser]
You could definitely add the injection functionality by extending the controller and adding some methods to register a callback internally, and listen for it using __call(). But it might be more complicated than it's worth for what you're doing.

If raw PHP is an option, that might be your best bet Smile




Theme © iAndrew 2016 - Forum software by © MyBB