Skip to content
This repository has been archived by the owner on Jul 14, 2021. It is now read-only.

Modulebuilder: form validation callback functions do not work #295

Closed
jmalinens opened this issue Jan 17, 2012 · 13 comments
Closed

Modulebuilder: form validation callback functions do not work #295

jmalinens opened this issue Jan 17, 2012 · 13 comments
Labels

Comments

@jmalinens
Copy link
Contributor

http://codeigniter.com/forums/viewthread/176150/#969627

We need to use

if ($this->form_validation->run($this) === FALSE)
instead of
if ($this->form_validation->run() === FALSE)
everywhere

@ghost
Copy link

ghost commented Jan 20, 2012

Hi @jmalinens

Where do they not work? In the Builder itself or in the generated module?

The no_match callback worked for me when I tried it in the Builder.

The documentation doesn't say anything about passing $this into the run method only a group.
http://codeigniter.com/user_guide/libraries/form_validation.html#savingtoconfig

Could you give some more info?

Thanks

@jmalinens
Copy link
Contributor Author

problem is in generated module

    public function captcha_check($nResult)
    {

            if ($nResult != 4)
            {
                    $this->form_validation->set_message('captcha_check', 'You have problems with basic math or you are spam robot...');
                    return FALSE;
            }
            else
            {
                    return TRUE;
            }

    }

    //--------------------------------------------------------------------
    //--------------------------------------------------------------------
    // !PRIVATE METHODS
    //--------------------------------------------------------------------

    /*
      Method: save_requests()

      Does the actual validation and saving of form data.

      Parameters:
      $type - Either "insert" or "update"
      $id       - The ID of the record to update. Not needed for inserts.

      Returns:
      An INT id for successful inserts. If updating, returns TRUE on success.
      Otherwise, returns FALSE.
     */
    private function save_requests($type='insert', $id=0) {

        $this->form_validation->set_rules('body', 'Request_text', 'required|trim|xss_clean|max_length[500]');
        $this->form_validation->set_rules('category', 'Request_category', 'required|trim|xss_clean|alpha');
        $this->form_validation->set_rules('e-mail', 'Email', 'trim|xss_clean|valid_email|max_length[45]');
        $this->form_validation->set_rules('stupid_captcha', 'Captcha', 'callback_captcha_check');
        $_POST['languages_id'] = $this->lang->lang_id();

        if ($this->form_validation->run($this) === FALSE) {
            return FALSE;
        }

without adding $this, callback function does not run

@ghost
Copy link

ghost commented Jan 20, 2012

Is that in one of your modules? I don't recognise the code but maybe I'm still half asleep :|

Are you requesting that the change be made to the Module Builder code itself or the module code that gets generated?

@jmalinens
Copy link
Contributor Author

Module generator should genereate $this->form_validation->run($this) everywhere instead of $this->form_validation->run().
It is HMVC specific "bug" and it seems is dating back to 2009:
http://codeigniter.com/forums/viewthread/126586/

this is something to do with Form_validation.php library line 588:

if ( ! method_exists($this->CI, $rule))

because of HMVC form validation library "can't see" if callback method exists or not so it skips running callback.

@og-shawn-crigger
Copy link
Contributor

I thought you just needed to add

 $this->form_validation->CI =& $this;   // Hack to make it work properly with HMVC

To make the form_validation library work with HMVC

@ghost
Copy link

ghost commented Jan 20, 2012

I'm not sure on this - which solution should we go with?

@svizion Where would your line of code go?

@og-shawn-crigger
Copy link
Contributor

<?php
    // With HMVC you need to pass into or set the controller instance for the library before using it.
    $this->load->library('form_validation');
    $this->form_validation->CI = $this;

    /* or */

    $this->load->library('form_validation', array('CI' => $this)); 
    // And then process the parameters inside the library constructor. 

That is what I always thought you had to do to make Form Validation work with HMVC or atleast that is what I found on CI Forums.

It's already in use in Auth and Admin_Controller.

@ghost
Copy link

ghost commented Jan 20, 2012

@svizion Ah indeed it is - thanks for that.

@jmalinens thanks for finding this but we will go with the way it has been resolved in Bonfire already.

Thanks

@ghost
Copy link

ghost commented Jan 20, 2012

Hmm so if it's already done in the Authenticated_Controller how is it still not working?

The form_validation is loaded before the module is loaded so where would we put the code in the module?

@og-shawn-crigger
Copy link
Contributor

Strange in the Forum post he referenced It says you can do it either way

Here is the solution…

$this->load->library('form_validation');
$this->form_validation->CI =& $this; 

or this

You can also use:

$this->load->library('form_validation');
if($this->form_validation->run($this)) { 

Strange since your using a captcha code, is this on the front_end controller ?

@jmalinens
Copy link
Contributor Author

Yes, front end controller:

class requests extends Front_Controller {

    //--------------------------------------------------------------------

    public function __construct() {
        parent::__construct();

        $this->load->library('form_validation');

We are taking too much time on this issue- not very important :D

@ghost
Copy link

ghost commented Jan 21, 2012

Ah right.

The front end controller is up to the developer really but I'll have a look at the code for it.

Thanks

@ghost ghost closed this as completed in 18a544b Jan 22, 2012
@ghost
Copy link

ghost commented Jan 22, 2012

@jmalinens @svizion That should be it now.

This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants