CodeIgniter Forums
Issu form_validation - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Issu form_validation (/showthread.php?tid=1355)



Issu form_validation - komang suryadana - 03-02-2015

PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
class 
Login extends CI_Controller {
  function 
__construct(){
    
parent::__construct();
    
$this->load->helper(array('form','url'));
    
$this->load->model('m_login');
    
$this->load->library('session''form_validation');
  }
  function 
index()
  {
    
$this->load->view('admin/v_login');
  }
  function 
valid(){
    
$data=array(
      
'username'=>$this->input->post('username'),
      
'password'=>$this->input->post('password')
      );
    
$this->form_validation->set_error_delimiters('<div class="error">''</div>');
    
$this->form_validation->set_rules('username''Username''trim|required|xss_clean');
    
$this->form_validation->set_rules('password''Password''trim|required|xss_clean');
    if (
$this->form_validation->run() == FALSE) {
        
$this->m_login->m_aksi($data);
        
$this->session->set_userdata($data);
        
redirect('admin/login/sukses''refresh');
    }else{
      
$this->load->view('admin/v_login');
    }
  }
  function 
sukses()
  {
    
redirect('admin/dasabor''refresh');
  }
  function 
logout(){
    
$this->session->unset_userdata('');
    
$this->load->view('admin/v_login');
  }


Error message :


A PHP Error was encountered

Severity: Notice

Message: Undefined property: Login::$form_validation

Filename: admin/login.php

Line Number: 19

Backtrace:

File: C:\xampp\htdocs\cms\application\controllers\admin\login.php
Line: 19
Function: _error_handler

File: C:\xampp\htdocs\cms\index.php
Line: 292
Function: require_once


RE: Issu form_validation - _this - 03-02-2015

Hello komang,

According to CI3 User Guide, look at Loader Class and you'll see that you can't pass two libraries as two string parameters. The class will try here to load the session class with a form_validation parameter, which is interpreted as a var that doesn't exist at all.

The good way is to chain load :

PHP Code:
$this->load->library('session')
 
          ->library('form_validation'); 

Or even the same array method that you used to load helpers in constructor Tongue


RE: Issu form_validation - CroNiX - 03-02-2015

Or (also in the userguide) use an array...
$this->load->library(array('session', 'form_validation'));


RE: Issu form_validation - komang suryadana - 03-02-2015

ok i am try
Tanks all


RE: Issu form_validation - komang suryadana - 03-03-2015

How to view specific error in form?


RE: Issu form_validation - Avenirer - 03-03-2015

It would be a good idea to rtfm... http://www.codeigniter.com/docs


RE: Issu form_validation - _this - 03-03-2015

@Avenirer : Agree with you, but it would be great to redirect him to the right page of userguide ^^

@komang: Here are form validation error handling Smile

You should add the UserGuide to your bookmarks !


RE: Issu form_validation - sandeep - 03-03-2015

Hi Komang,

You can Load session library in autoload.php as well. As you have to use session in maximum pages.


RE: Issu form_validation - komang suryadana - 03-04-2015

Help me to use xss_clean in form_validation?
i am trying still an error as below.
Unable to access an error message corresponding to your field name Username.
Unable to access an error message corresponding to your field name Password.

this my code to use xss_clean:
PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class 
Login extends CI_Controller {
 
 function __construct(){
 
   parent::__construct();
 
   $this->load->helper(array('form','url'));
 
   // $this->load->model('m_login');
 
   $this->load->library(array('session''form_validation'));
 
 }
 
 function index()
 
 {
 
   $this->load->view('admin/v_login');
 
 }
 
 function valid(){
 
   $this->form_validation->set_error_delimiters('<div class="merah">''</div>');
 
   $this->form_validation->set_rules('username''Username''required|xss_clean');
 
   $this->form_validation->set_rules('password''Password''required|xss_clean');
 
   if ($this->form_validation->run() == TRUE) {
 
     $data=array(
 
     'username'=>$this->input->post('username'),
 
     'password'=>$this->input->post('password'
 
     );
 
     if ($data['username'] == 'admin' && $data['password'] == '1234') {
 
       $this->session->set_userdata($data);
 
       redirect('admin/login/sukses');
 
     }else{
 
       return TRUE;
 
     }

 
     
    
}else{
 
     $this->load->view('admin/v_login');
 
   }
 
 }
 
 function sukses()
 
 {
 
   redirect('admin/dasabor''refresh');
 
 }
 
 function logout(){
 
   $this->session->sess_destroy();
 
   $this->load->view('admin/v_login');
 
 }


and i set
PHP Code:
$config['global_xss_filtering'] = TRUE
Huh


RE: Issu form_validation - CroNiX - 03-04-2015

You'd also need to show us the view where you are trying to display the error messages. You also don't use xss_clean for a validation rule. More recent versions of CI removed that. See the available rules in the CI3 userguide.