Welcome Guest, Not a member yet? Register   Sign In
Extend Form_Validation
#1

(This post was last modified: 04-04-2016, 06:53 AM by lucavalentino.)

I use Codeigniter 3.0.6
I want extend the library Form_Validation but not received a personal message error

MY_validation extends CI_Form_validation
PHP Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class 
MY_validation extends CI_Form_validation {
    function MY_validation(){
        parent::__construct();
}
    function url($str){
        $pattern "|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i";
        if (!preg_match($pattern$str)){
            $this->My_validation->set_message('url''The URL you entered is not correctly formatted.');
            return FALSE;
    }
        return TRUE;
    }


My controller
PHP Code:
class valid extends MY_Controller{
    public function 
__construct(){
    
parent::__construct();
        
$this->_CI =& get_instance();
        
$this->_CI->lang->load('upload');
        
$this->load->model('upload_m');
    }
    public function 
index(){
        
$this->load->helper(array('form''url'));
        
$this->load->library('form_validation','My_validation');
        
$this->validation->CI =& $this;
......
......
     
$this->form_validation->set_rules('sito''Sito''url');
        if (
$this->form_validation->run()==FALSE){
$this->load->view('upload_edit_view); }
}


In the View print an errors echo validation_errors(); see {url} and not  personal message.

Help me
Reply
#2

You should use visibility attribute in parent class.
Since version 5.3.3 (which is some 6 years now), method with same name as class are replaced with __construct().
Class names in CI3 should be ucfirst treated.
You should make it in way docs propose `MY_Form_validation extends CI_Form_validation{}`.
Prefix `MY_` shouldn't be used when file is called/class is loaded.
`$this->My_validation->set_message` can be `$this->form_validation->set_message`.
Reply
#3

It should also be named MY_Form_validation not MY_validation.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(This post was last modified: 04-04-2016, 02:09 PM by lucavalentino.)

I changed the library as suggested and etc.
PHP Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class 
MY_Form_validation extends CI_Form_validation {
 protected 
$CI;
 function 
__construct() {
 
 parent::__construct();
 
 $CI =& get_instance();
 }
 function 
url($str){
 
 $pattern "|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i";
 
 if (!preg_match($pattern$str)){
 
  $CI->form_validation->set_message('url''The URL you entered is not correctly formatted.');
 
  return FALSE;
 
  
 
  else {
 
   return TRUE;
 
  }
 
   }


The library still do not work.
Help me, I can not continue create the application
Reply
#5

You don't need to create separate variable for CI instance if you are extending CI system class.
Socondly, when loading your newly created class, you don't need prefix 'MY_'.

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

class 
MY_Form_validation extends CI_Form_validation
{
    public function 
__construct()
    {
 
       parent::__construct();
    }

    public function 
url($str)
    {
 
       $pattern "|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i";
 
       if (!preg_match($pattern$str))
        {
 
           $this->form_validation->set_message('url''The URL you entered is not correctly formatted.');
 
           return FALSE;
 
       
 
       else
        {
         
   return TRUE;
   
     }
 
   }


Controller example code:

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

class 
Example_c extends CI_Controller
{
 
   public function __construct()
 
   {
 
       parent::__construct();
        
$this->load->library('form_validation');
 
   }

 
   public function index()
 
   {
 
       //echo 'Example';
 
   }

Reply
#6

(This post was last modified: 04-05-2016, 03:07 AM by lucavalentino.)

I try but not work.
I am using Codeigniter 3.0.6 (last version)

(04-04-2016, 03:32 PM)Tpojka Wrote:
PHP Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class 
MY_Form_validation extends CI_Form_validation
{
 
   public function __construct()
 
          parent::__construct();    }

 
   public function url($str)
 
   {
 
       $pattern "|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i";
 
       if (!preg_match($pattern$str))
 
       {
 
           $this->form_validation->set_message('url''The URL you entered is not correctly formatted.');
 
           return FALSE;
 
       
 
       else
        
 return TRUE   }
 
   }


Controller example code:

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

class 
Example_c extends CI_Controller
{
 
   public function __construct()
 
   {
 
       parent::__construct();
 
       $this->load->library('form_validation');
 
   }

 
   public function index()
 
   {
 
       //echo 'Example';
 
   }


I believe that the controller does not read extensive library, but only the message from the language file
Reply
#7

What I showed you is basic example from docs.
You should test most simplified example like one in docs and see what (and if) went wrong.
I already linked that page/section before.
Reply
#8

PHP Code:
$this->CI =& get_instance(); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#9

(This post was last modified: 04-05-2016, 09:18 AM by lucavalentino.)

(04-05-2016, 05:52 AM)InsiteFX Wrote:
PHP Code:
$this->CI =& get_instance(); 

I try :

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

class 
MY_Form_validatio extends CI_Form_validation
{
 
   public function __construct($config = array())
 
   {
 
       parent::__construct($config);
 
       $this->_CI =& get_instance(); 
 
   }

 
   public function url_formato($str)
 
   {
 
       $pattern "|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i";
 
       if (!preg_match($pattern$str))
 
       {
 
         $this->_CI->form_validation->set_message('url_formato''The URL you entered is not correctly formatted.');
 
         return FALSE;
 
       }
 
       else
        
{
 
        return TRUE;
 
       }
 
   }

I believe that the controller does not read my extensive library

Now to continue the application I using callback function.
Reply
#10

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

class 
MY_Form_validatio extends CI_Form_validation

should be
:
class 
MY_Form_validation extends CI_Form_validation
{
    private 
$_CI;

 
   public function __construct($config = array())
 
   {
 
       parent::__construct($config);
 
       $this->_CI =& get_instance(); 
 
   }

 
   public function url_formato($str)
 
   {
 
       $pattern "|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i";
 
       if (!preg_match($pattern$str))
 
       {
 
         $this->_CI->form_validation->set_message('url_formato''The URL you entered is not correctly formatted.');
 
         return FALSE;
 
       }
 
       else
        
{
 
        return TRUE;
 
       }
 
   }

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB