[eluser]ToddyBoy[/eluser]
I was hoping this would solve my problem as well, but it didn't.
I'm getting the validation errors appearing in my view before the validation is being called.
Here's my controller:
Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');
if ( ! class_exists('Controller'))
{
class Controller extends CI_Controller {}
}
class Entrar extends Controller {
function __construct()
{
parent::__construct();
$this->load->library('file_upload');
$this->load->library('session');
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
}
public function test()
{
// Validate form input
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->form_validation->set_rules('password', 'Password', 'trim|required');
if ($this->form_validation->run() == true){
// If the validation is successful
$this->session->set_flashdata('message', 'Validation successful');
redirect('entrar', 'refresh');
} else {
// Display the form
// Set the flash data error message if there is one
$this->data['message'] = (validation_errors()) ? validation_errors() : $this->session->flashdata('message');
$this->data['username'] = array('name' => 'username',
'id' => 'username',
'type' => 'text',
'value' => $this->form_validation->set_value('username'),
);
$this->data['password'] = array('name' => 'password',
'id' => 'password',
'type' => 'password',
);
$this->load->view('media/test', $this->data);
}
}
Here's my view:
Code:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<meta name="Description" content="" />
<meta name="Keywords" content="" />
</head>
<body>
<?php if ($message) { if($message == "<p>Validation successful</p>"){ ?>
<div id="success"><?php echo $message;?></div>
<?php } else { ?>
<div id="error"><?php echo $message;?></div>
<?php } } ?>
<?php echo form_open("entrar");?>
<fieldset>
<label for="username">Username</label>
<?php echo form_input($username);?>
<br>
<label for="password">Password</label>
<?php echo form_input($password);?>
<br>
<label for="remember">Remember Me</label>
<?php echo form_checkbox('remember', '1', FALSE, 'id="remember"');?>
<br>
<?php echo form_submit('submit', 'Login', 'id="submit"');?>
</fieldset>
<?php echo form_close();?>
</body>
</html>
Here's the rendered html when the page loads with the error messages appearing:
Code:
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title></title>
<meta name="Description" content="" />
<meta name="Keywords" content="" />
</head>
<body>
<div id="error"><p>The Username field is required.</p>
<p>The Password field is required.</p>
</div>
<form action="https://mysite.com/entrar" method="post" accept-charset="utf-8"> <fieldset>
<label for="username">Username</label>
<input type="text" name="username" value="" id="username" /> <br>
<label for="password">Password</label>
<input type="password" name="password" value="" id="password" /> <br>
<label for="remember">Remember Me</label>
<input type="checkbox" name="remember" value="1" id="remember" /> <br>
<input type="submit" name="submit" value="Login" id="submit" /> </fieldset>
</form>
</body>
</html>
I've used this code on another installation and it works fine. The other installation I KNOW is 2.1.0. This current installation I THINK is 2.0.2.
I downloaded a 2.1.0 installation from github and copied the form_validation helper from 2.1.0 into the not working version and I didn't get anything rendered at all. So I backed out that change and am back to square one.
When I actually go through the form and enter a username, for example, and click submit, I only get the error for the password being required. Entering data into both fields, I get the successful validation message.