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.
#2

[eluser]Sven Delle[/eluser]
So I:

1: Installed a clean CI 2.1.0.
2: Downloaded and implemented HMVC according to wiki.
3: Extended Form_validation class according to wiki.
4: Went through the Form Validation Tutorial, step by step.

Result: Absolutely NO callbacks!

What am I doing wrong - I assume I'm doing SOMETHING wrong - otherwise HMVC DO NOT WORK WITH callbacks!

This is my Controller:

Code:
<?php
class Admin extends CI_Controller
{
public function __construct()
{
  parent::__construct();
  $this->load->library('form_validation');
  $this->form_validation->CI =& $this;
}

public function index()
{  
  $this->load->view('login_form');
}

public function validate_user()
{
  $this->load->helper(array('form', 'url'));
  
  $this->form_validation->set_rules('username', 'Username', 'required', 'callback_username_check');
  $this->form_validation->set_rules('password', 'Password', 'required');
  $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
  $this->form_validation->set_rules('email', 'Email', 'required');

  if($this->form_validation->run() === FALSE)
  {
   $this->load->view('login_form');
  }
  else
  {
   $this->load->view('dashboard');
  }
}

public function username_check($str)
{
  echo 'Hello World!'; // This NEVER gets called!!!
  
  if ($str == 'test')
  {
   $this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
   return FALSE; // I've tried with "AAAAARGH!"
  }
  else
  {
   return TRUE; // I've also tried this with "AAAAARGH!"
  }
}
}

Someone please help me, as I'm in the middle of the night. A was supposed to get things done - not trying to fix things.
#3

[eluser]CroNiX[/eluser]
You will have better luck asking this question in the thread created by the HVMC Wiki.
#4

[eluser]Sven Delle[/eluser]
OK!

Big apologies to all for freaking out in various post about this issue.

But I'll post my 'bug' here, as someone else could be doing the same mistake as me.

I tried (unintentionally) to have both a 'required' rule and a 'callback_' at the same time (idiot me)!

so instead of:
Code:
$this->form_validation->set_rules('username', 'Username', 'callback_check_username');

I had:
Code:
$this->form_validation->set_rules('username', 'Username', 'required', 'callback_check_username');

So, make sure your third parameter is the callback (beating myself). There's a couple of hours truly wasted!

Hope this post helps someone some day!
#5

[eluser]Sven Delle[/eluser]
Is it possible to have both a required and a callback at the same time somehow?

If I use a callback to check for existing username I cannot send any errors to the validator if the field is empty. I doesn't validate, but I'd like to send it the required error.

Any solution for that?

Well, yes:
Code:
$this->form_validation->set_rules('username', 'Username', 'required|callback_check_default_username');




Theme © iAndrew 2016 - Forum software by © MyBB