• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I put form callbacks in a helper file?

#1
[eluser]Ninjabear[/eluser]
Hi. I've been copying my callbacks from controller - controller every time I use them. This means duplicating code which I don't like. I tried to put them all in a helper which I called callback_helper.php. I've used helpers before so I know how to make them work but for some reason the form_validation class is not seeing them. Also the callbacks are working when they are in the controller just in case you were wondering.

Code:
$autoload['helper'] = array('url','form','content','callback');
The helper is at: application/helpers/callback_helper.php

I'm thinking this could be a load-order thing. Are helpers loaded before controllers?
Maybe I shouldn't use a helper but some kind of class?

BTW: I searched google / CI forum but I couldn't find anything useful on this subject.

My helper in case you need it:

Code:
<?php

/*
* callback_helper.php
* @package content
*/

/**
  * _not_selectable
*
* @access public
* @param array values to print.
* @return true.
*/

  //Not selectable is useful when there are values in a select which cannot be selected as valid option. Second param is a piped string of primary key ids from the task_types db table. $option param = Value from select.
  function _not_selectable($option,$values)
  {
    $not_selectable = explode('|',$values);
    
    if(in_array($option,$not_selectable))//Is the value invalid?
    {
      $this->form_validation->set_message('_not_selectable', 'That option is not selectable.');
      return false;
    }    
    return true;
  }

/**
  * _Valid_Date_Format
*
* @access public
* @param array values to print.
* @return true.
*/
    
  //Check date format aggainst mysql format, valid dates only, no 30-2-2010.
  function _Valid_Date_Format($date)
  {
    $converted=str_replace('/','-',$date);

    if(preg_match("/^((((19|20)(([02468][048])|([13579][26]))-02-29))|((20[0-9][0-9])|(19[0-9][0-9]))-((((0[1-9])|(1[0-2]))-((0[1-9])|(1\d)|(2[0-8])))|((((0[13578])|(1[02]))-31)|(((0[1,3-9])|(1[0-2]))-(29|30)))))$/",$converted)===1)
    {
      return TRUE;
    }

    $this->form_validation->set_message('_Valid_Date_Format', 'The Date entered is invalid.');
    return FALSE;
  }  


/* End of file callback_helper.php */
/* Location: ./application/helpers/callback_helper.php */

#2
[eluser]Matalina[/eluser]
I don't know if there is another way, but I create a library validation method per usual. And then for each call back in the controller, load the helper and call the helper I need inside of the controller.

#3
[eluser]Ninjabear[/eluser]
[quote author="Matalina" date="1331129025"]I don't know if there is another way, but I create a library validation method per usual. And then for each call back in the controller, load the helper and call the helper I need inside of the controller. [/quote]

Sounds interesting but I don't know what you mean by library validation methods. I found an article here:

http://www.scottnelle.com/41/extending-c...n-library/

Are you talking about extending the CI validation class or some other thing?

#4
[eluser]Matalina[/eluser]
No. You said you wanted to but your callback functions in a library.

In your library you would have your validation call back login. In your controller you would load said library and call the call back function.

#5
[eluser]CroNiX[/eluser]
Why don't you just extend CI's validation library with your own rules?

/application/libraries/MY_Form_validation.php
Code:
class MY_Form_validation extends CI_Form_validation {
  function __construct()
  {
    parent::__construct():
  }

  // don't need to user underscore to make private since its
  // in the library now and not your controller
  function Valid_Date_Format($date)
  {
    $converted=str_replace('/','-',$date);

    if(preg_match("/^((((19|20)(([02468][048])|([13579][26]))-02-29))|((20[0-9][0-9])|(19[0-9][0-9]))-((((0[1-9])|(1[0-2]))-((0[1-9])|(1\d)|(2[0-8])))|((((0[13578])|(1[02]))-31)|(((0[1,3-9])|(1[0-2]))-(29|30)))))$/",$converted)===1)
    {
      return TRUE;
    }
    // Don't need to reference the form_validation object since that is now $this
    $this->set_message('Valid_Date_Format', 'The Date entered is invalid.');
    return FALSE;
  }  
}

Now when setting your rules, you just use the rule name "as is" like the native rules
instead of "callback_rule_name".
Code:
$this->form_validation->set_rules('field', 'Field', 'required|Valid_Date_Format');


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.