Welcome Guest, Not a member yet? Register   Sign In
CodeIgniter 3 - Extending Form Validation not working
#1

MY_Form_validation.php in the folder application/libraries/MY_Form_validation.php
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Form_validation extends CI_Form_validation
{
//protected $CI;
public function __construct($config = array())
   {
parent::__construct($config);
$this->CI =& get_instance();
$this->_config_rules = $config;
   }

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

  /**
* required_if
*
* @access public
* @param string $str
* @param string $field
* @return bool
*/
function required_if($str, $field)
{
$this->CI->my_form_validation->set_message('required_if', $this->CI->lang->line('con_the').' '.'%s'.' '.$this->CI->lang->line('not_field_required'));
if ( ! is_array($str))
{
return (trim($str) == '') ? FALSE : TRUE;
}
else
{
return ( ! empty($str));
}
}

//-----------------------------------------------------------
}

?>
--------------------------------------------------

my view file:
===========

<!-- start - forms -->
<div class="forms lt" style="width:49%">
<?php
$form_attributes = array(
"id" => "reg_form",
"name" => "reg_form",
"method" => "post",
);
echo form_open(base_url()."user/login", $form_attributes);
?>
   <div class="subtitle"><?=$this->lang->line('con_login')?></div>
   
   <!-- start - form_fields -->
   <div class="form_fields">
   
       <div class="row">
           <div class="label"><?=$this->lang->line('con_username')?> / <?=$this->lang->line('con_email')?> <span class="mand">*</span></div>
           <div class="field"><input id="username" name="username" type="text" value="<?= set_value('username') ?>" /></div>
           
       </div>
       
       <div class="row">
           <div class="label"><?=$this->lang->line('con_password')?> <span class="mand">*</span></div>
           <div class="field"><input id="password" name="password" type="password" value="<?= set_value('password') ?>" /></div>
         
       </div>
       
       <div class="row">
          <input type="submit" id="submit" name="submit" value="login"  />
       </div>

   </div>
   <!-- end - form_fields -->
   <?=form_close()?>
</div>
<!-- end - forms -->

----------------------------------------------------------

my controller function:
===============

class User extends MY_Controller {

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

function login()
{
$this->CI =& get_instance();
$this->CI->load->library('my_form_validation');
if( ! $this->session->userdata('logged_in'))
{
if($this->input->post('submit'))
{
$config_rules = array(
array(
'field' => 'username',
'label' => $this->lang->line('con_username'),
'rules' => 'required_if' ),
array(
'field' => 'password',
'label' => $this->lang->line('con_password'),
'rules' => 'required_if'),
);

$this->CI->my_form_validation->set_rules($config_rules);  
$this->CI->my_form_validation->set_error_delimiters('<span class="terr">', '</span>');
if($this->form_validation->run())
{}
}      }        }
}

I got following errors

A PHP Error was encountered

Severity: Notice

Message: Undefined property: User::$my_form_validation

Filename: user/login.php

Line Number: 15


( ! ) Fatal error: Call to a member function error() on a non-object in D:\wamp\www\getcommun\application\views\user\login.php on line 15

A PHP Error was encountered

Severity: Error

Message: Call to a member function error() on a non-object

Filename: user/login.php

Line Number: 15

Any please help me its very urgent.....
Reply


Messages In This Thread
CodeIgniter 3 - Extending Form Validation not working - by grsabarish - 09-02-2015, 10:59 PM



Theme © iAndrew 2016 - Forum software by © MyBB