Welcome Guest, Not a member yet? Register   Sign In
Validate form with default values?
#1

[eluser]Sven Delle[/eluser]
Hi,

I'm trying to validate a form which has some default data in the input fields. I've followed the documentation exactly as I can (may be overruled on that one).

But I can't seem to invoke ANY of the errors in my callback functions (if I clear both username and password fields i DO get the 'required error', so form_validation seems to be working fine.)

Can anyone see why I don't get any errors if the input fields containing the default values?

I'm using the HMVC approach in case this matter.

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

class Admin_login extends CI_Controller
{
public function __construct()
{
  parent::__construct();
}
  
public function index()
{
  // Prepare and load Login Form
  $this->data['site_title'] = 'Site Title';
  $this->data['head'] = 'head';
  $this->data['css'] = array('assets/admin/css/fonts.css', 'assets/admin/css/base.css', 'assets/admin/css/form.css');
  $this->data['javascript'] = array('assets/admin/js/form.js');
  $this->data['main_content'] = 'login_form';
  $this->load->view('includes/page', $this->data);
}

public function validate_user()
{
  // FORM AND URL HELPERS ARE AUTO LOADED
  // Load libraries
  $this->load->library('form_validation');
  
  // Load models
  $this->load->database();
  $this->load->model('login/user_model');
  
  // Set validation rules
  $this->form_validation->set_rules('username', 'Username', 'required|xss_clean', 'callback_check_default_username');
  $this->form_validation->set_rules('password', 'Password', 'required|xss_clean', 'callback_check_default_password');
  
  // Check for validation
  if($this->form_validation->run() === FALSE)
  {
   // If validation fails - reload Login Form
   $this->index();
   // Also tried
   //$this->load->view('login_form');
  }
  else
  {
   // If form_validation succeeds - validate user
   $query = $this->user_model->validate_user();
  
   // If user validates - set userdata and load Dashboard
   if($query)
   {
    $data = array(
     'username' => $query->username,
     'screen_name' => $query->screen_name,
     'logged_in' => true,
     'access_level' => $query->access_level
    );

    $this->session->set_userdata($data);
    $this->load->view('dashboard');
   }
   else
   {
    // If user do not validate - reload Login Form
    $this->index();
   }
  }
}

public function check_default_username($str)
{
  if($str == 'Username')
  {
   // I've also tried to set first parameter to 'username' and 'password' respectively to match the rules, but no luck
   $this->form_validation->set_message('check_default_username', 'Field %s cannot be "Username"');
   return FALSE;
  }
  else
  {
   return TRUE;
  }
}
public function check_default_password($str)
{
  if($str == 'Password')
  {
   $this->form_validation->set_message('check_default_password', 'Field %s cannot be "Password"');
   return FALSE;
  }
  else
  {
   return TRUE;
  }
}
}
/* End of file: ./application/modules/admin/controllers/admin_login.php */

Read other posts on how to extend Form_validation with MY_Form_validation in the /application/libraries, but that changed absolutely nothing.


Messages In This Thread
Validate form with default values? - by El Forum - 04-18-2012, 12:58 PM
Validate form with default values? - by El Forum - 04-18-2012, 04:49 PM
Validate form with default values? - by El Forum - 04-18-2012, 04:53 PM
Validate form with default values? - by El Forum - 04-18-2012, 05:39 PM
Validate form with default values? - by El Forum - 04-18-2012, 05:55 PM



Theme © iAndrew 2016 - Forum software by © MyBB