CodeIgniter: custom validation rules can't be in a helper? - codeigniter

I've created a "validation helper" that was supposed to have all my custom validation rules. The problem is that when I use them in my form validation, they seem to be ignored. If I move the functions in the controller that is doing the form validation, everything works like a charm. My validation helper is autoloaded.
Is there any reason why I can't seem to use these validation functions if I put them in a helper? Thanks.

A function in a helper and a controller are different obviously.
Create an extended MY_Form_validation.php in your libraries/, add the functions there and finally set the rules without callback_ and just their function name.
Example:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Form_validation extends CI_Form_validation {
/* set_rule('custom_require') */
function custom_require($str) {
return (bool)$str;
}
}

Robin's answer is the easiest way to deal with it; however, the why you can't is this:
look in your system/libraries/Form_Validation.php, line: 587
if ( ! method_exists($this->CI, $rule))
{
continue;
}
This check is done on all callbacks. Helpers are not classes & not loaded into the CI instance - and so not available from the Form_Validation library (because of the way it is specifically coded in this method)

Related

Codeigniter(HMVC) core classes is not loaded in pre_contoller hook

I have downloaded the ci-hmvc-master and setup it.
Now in that I have created a one hook at pre_controller hook point.
In that hook, I have created one function that print the current class and method.
my code looks like this:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Authentication{
protected $CI;
public function __construct() {
$this->CI = & get_instance();
}
public function is_loggedin(){
$class = $this->CI->router->fetch_class();
echo $class;
}
}
This code is working fine. I got the class name.
Now my issue is that when I use this same code in HMVC which is created from normal MVC (replacing some of the folders and files) then I got the error like:
Call to a member function fetch_class() on null
Means The pre_controller hook executes before the super object has been fully constructed, so get_instance() can't work.
So, why it is working in HMVC extension which I have downloaded and why it is
not working in simple HMVC which is created from MVC.
Is there any configuration is missing?

CodeIgniter URI Routing: remove method name index from URL

I am working with CodeIgniter (V:2.2.6) and I have a simple class User with some basic methods like create, update, edit, delete and index. For the index function, I am using an argument $user (which is the second URI segment) in order to display some information regarding that user. So the default URL looks like:
/user/index/john
to display some information about the user 'john'.
Now, I want to remove the term 'index' from the URL, so that it looks like:
/user/john
For that purpose I have added the following rule in routes.php.
$route['user/(:any)'] = "user/index/$1";
It serves the purpose, but it prevents accessing other functions like /user/create and goes inside /user/index automatically. To solve this problem, I can not see any other way except manually adding routing rules like
$route['user/create'] = "user/create";
for each method of the User class, which is not cool at all. So, please can anyone suggest me a better way of routing under the current circumstances?
Here are my codes:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class User extends CI_Controller {
public function index($user = '') {
echo "index!";
}
public function create() {
echo 'create!';
}
}
Note: I have gone through the CodeIgniter documentation for URI Routing and another similar question here, but could not figure out a promising solution. And please don't suggest for CodeIgniter version update.
I'm not sure, if this is the answer you like, but i think it should work.
Just put this function in your user controller.
public function _remap($method)
{
if (method_exists($this, $method))
{
$this->$method();
}
else
{
$this->index($method);
}
}

changing the url into dash instead of underscore using Codeigniter

Hi im using codeigniter framework because its really cool when it comes to MVC thing. I have a question regarding in the url thing. Usually when saving a controller, lets say in the about us page it has to do something like this About_us extends CI_Controller. When that comes to the url thing it goes like this test.com/about_us. I want that my url is not underscore. I want to be dashed something like this test.com/about-us. How will i able to use the dash instead of using underscore???
any help is greatly appreciated
thanks!
Code Ignitor 3 has this built-in option. In your routes.php:
$route['translate_uri_dashes'] = FALSE;
Just change this to "TRUE" and you can use either _ or - in URL
Here is a detailed article how to fix it.
You will just have to create routes for your pages, as far as I am aware, there is no config directive to change the separating character, or replace '-' with '_' in CI_Router, (probably wouldn't be too hard to add this).
To allow for '-' instead of '_':
Create a file in application/core/MY_Router.php
Change 'MY_' to whatever value is specified in your config directive: $config['subclass_prefix']
insert code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Router extends CI_Router {
function _set_request($segments = array()) {
if (isset($segments[0]))
$segments[0] = str_replace('-','_',$segments[0]);
if (isset($segments[1])) {
$segments[1] = str_replace('-','_',$segments[1]);
}
return parent::_set_request($segments);
}
}

Extending Form Validation in Codeigniter call to a member function on a non-object error

I know this has been asked before, and I've looked through every answer posted, but to no avail. Basically, I am trying to extend the Codeigniter form validation library on a CI2+ project, but every single time I get the error:
Fatal error: Call to a member function get_errors_array() on a non-object
Below is my code:
application/core/CB_Form_validation.php
class CB_Form_validation extends CI_Form_validation{
function __construct($rules = array()) {
parent::__construct($rules);
log_message('debug', "CB_Form_validaiton class initialized");
}
public function get_errors_array() {
return $this->_error_array;
}
}
and then in my ajax file I have the construct etc.
public function save(){
if($this->form_validation->run() == FALSE){
}
}
and inside that if statement I have tried the following:
echo $this->form_validation->get_errors_array();
echo $this->cb_form_validation->get_errors_array();
echo get_errors_array();
I have also tried placing the CB_Form_validation.php in application/libraries as well. Just as well, I have also tried this in my construct of my ajax controller
$this->load->library('CB_Form_validation');
$this->load->library('core/CB_Form_validation');
And I have set CB_ as my custom class prefix
Turns out that to fix this, you should do the following:
Move your custom form validation class to the application/libraries folder
You can keep the construct of your custom class how it is
Use $this->form_validation->method() to access the function you would like
As long as your loading the form_validation library, you don't need to perform $this->load->library('custom_class') because CI picks up on your custom prefix anyways
Hope this helps someone

how to call method of other module?

I am new in the CodeIgniter framework and PHP. I am trying to call a method which is in another module's controller. For that I am using:
modules::run('addons/demo');
but it does not work. How can I accomplish this task?
include it and it will work but it is a bad practice to use this. Instead create a library and define the method there and it will be accessible in the whole application where ever you load the library. in the application/library create my_library.php
<?php Class My_library{
function common_method(){
echo 'this is a common method';
}
}
And call it in the controller method
<?php Class test_controller extends CI_Controller{
function __construct(){
parent::__construct();
}
function index(){
$this->load->library('my_library');
$this->my_library->common_method();
}
}
First just need to load the module
$this->load->module("module_name");
Then call the controller method from the loaded module.
$this->module_name->method_name();

Resources