Welcome Guest, Not a member yet? Register   Sign In
Custom Validation Function
#1

Hi guys,

Ive created a function(MyValidation) to validate the input format.

MyValidation function is I'm the same controller as my validation. I am able to run the validation by "callback_MyFunction". withOut any issue.

Now I'm trying to bring it out of my controller(Ive created a new class in library) that i can use this for another controllers without retype in new controller.

My validation to call MyValidation from library
PHP Code:
$this->form_validation->set_rules('input''lang:''trim|required|callback_'.$this->functions->MyValidation($this->input->post('input')) ); 
Also I've tried these 2 ways
PHP Code:
$this->form_validation->set_rules('input''lang:''trim|required|callback_'.$this->functions->MyValidation ); 
PHP Code:
$this->form_validation->set_rules('input''lang:''trim|required|callback_'.$this->functions->MyValidation() ); 

Any idea?
Reply
#2

The easiest thing to do is to create that function in a helper, not a library. As long as the helper is loaded, it will be able to find the function just like it can find any PHP command.

A slightly more "proper" way to do it would be to extend the Form_validation library in a MY_Form_validation file. Add the functions to that, and then you should be able to use it like:

Code:
$this->form_validation->set_rules('input', 'lang:', 'trim|required|my_validation' );
Reply
#3

(09-22-2015, 06:52 AM)kilishan Wrote: The easiest thing to do is to create that function in a helper, not a library. As long as the helper is loaded, it will be able to find the function just like it can find any PHP command.

A slightly more "proper" way to do it would be to extend the Form_validation library in a MY_Form_validation file. Add the functions to that, and then you should be able to use it like:


Code:
$this->form_validation->set_rules('input', 'lang:', 'trim|required|my_validation' );

"Kilishan" Thank you for reply.
I prefer to user as helper. will test and get back here to share my results.
Reply
#4

Ok now I'm trying to have my function in a helper inside helpers/ folder.
But I'm getting this error

Code:
An Error Was Encountered
Unable to load the requested file: helpers/validation_helper.php

and my helper written like this
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if (!
function_exists('Time_Validation'))
{
    function 
Time_Validation($data FALSE)
    {
 
            //...
 
       }


Ive tried to load my loader like
PHP Code:
$this->load->helper('validation'); 
And
PHP Code:
$this->load->helper('validation_helper'); 

My helper file started with small letter and I've tried to make it upper case (only the 'V') but still is the same.

Any Idea?

Thanks everybody
Reply
#5

Where in your controller are you loading the helper? If you are loading it in a _constructor you must call parent::__constructor like so:

PHP Code:
public function __constructor() {
 
   parent::__constructor();

 
   $this->load->helper('validation');

and load resources after this call else the helper or any resource you try to load will not be assigned to the CI super object
Reply
#6

(09-30-2015, 02:37 AM)Martin7483 Wrote: Where in your controller are you loading the helper? If you are loading it in a _constructor you must call parent::__constructor like so:


PHP Code:
public function __constructor() {
 
   parent::__constructor();

 
   $this->load->helper('validation');

and load resources after this call else the helper or any resource you try to load will not be assigned to the CI super object

Mine is also the same. I written in my construct function.
but i can see you written here constructor() instead of construct() !
is it true? i never seen contractor in CI. make me correct if I'm wrong.

Thanks for reply
Reply
#7

(This post was last modified: 09-30-2015, 06:42 AM by Martin7483.)

(09-30-2015, 03:13 AM)ardavan Wrote:
(09-30-2015, 02:37 AM)Martin7483 Wrote: Where in your controller are you loading the helper? If you are loading it in a _constructor you must call parent::__constructor like so:



PHP Code:
public function __constructor() {
 
   parent::__constructor();

 
   $this->load->helper('validation');

and load resources after this call else the helper or any resource you try to load will not be assigned to the CI super object

Mine is also the same. I written in my construct function.
but i can see you written here constructor() instead of construct() !
is it true? i never seen contractor in CI. make me correct if I'm wrong.

Thanks for reply

Sorry! That was a typo... It must be:
PHP Code:
public function __construct() {
 
   parent::__construct();
 
   $this->load->helper('validation');


The darn things are called constructors. But thats not what you place in your classes Tongue

Anyway, you say you are doing things this way in your controller?
Reply
#8

(This post was last modified: 09-30-2015, 07:23 AM by Martin7483.)

(09-22-2015, 06:52 AM)kilishan Wrote: A slightly more "proper" way to do it would be to extend the Form_validation library in a MY_Form_validation file. Add the functions to that, and then you should be able to use it like:


Code:
$this->form_validation->set_rules('input', 'lang:', 'trim|required|my_validation' );

I would advise you to use this approach.

First of all, it keeps all your code related to form validation together.
Second, you can easily override the default rule methods with your own.

Here is an example of a MY_Form_validation.php:

PHP Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * My Form validation
 * 
 * This class extends the CodeIgniter Form validation library.
 */

class MY_Form_validation extends CI_Form_validation {
 
 
   /**
     * __get
     *
     * Enables the use of CI super-global without having to define an extra variable.
     *
     * @access public
     * @param $var
     * @return mixed
     */
 
   public function __get($var) {
 
       return get_instance()->$var;
 
   }

 
   public function my_validation($str) {
 
       // Do whatever you need to do
 
   }
 

Reply




Theme © iAndrew 2016 - Forum software by © MyBB