[eluser]The Wizard[/eluser]
Hello Friends
ive a error in this file:
Code: <?php
$this->load->library('validation');
?>
<html>
<head>
<title>Register</title>
</head>
<body>
<?php echo $this->validation->error_string; ?>
<?php echo form_open('user/register'); ?>
<h5>Username</h5>
<input type="text" name="username" value="<?php echo $this->validation->username;?>" size="50" />
<h5>Password</h5>
<input type="text" name="password" value="" size="50" />
<h5>Password Confirm</h5>
<input type="text" name="passconf" value="" size="50" />
<h5>Email Address</h5>
<input type="text" name="email" value="" size="50" />
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>
it says:
A PHP Error was encountered
Severity: Notice
Message: Undefined property: CI_Validation::$username
Filename: views/register_new.php
Line Number: 25
" size="50" />
and the controller looks like:
Code: <?php
class User extends Controller
{
function __construct()
{
parent::Controller();
/* load template parser */
$this->load->library( 'parser' );
$this->load->model ( 'User_model' );
$this->load->database();
}
function register_process( $data )
{
/* add additional data, or form the data like you want. */
$data['status'] = "Pending";
$data['activation'] = 31337 + rand( 0, 666666999);
$this->load->helper('date');
$data['datetime'] = mdate( "%Y-%m-%d %H:%i:%s" );
$result_register = $this->User_model->User_Register( $data );
return $result_register;
}
function register ()
{
$this->load->helper( 'form' );
$this->load->helper( 'url' );
$this->load->library('validation');
/* by default the error messages are displayed in <p> tags. */
//$this->validation->set_error_delimiters('<div class="error">', '</div>');
//$rules['username'] = "callback_database_check";
$rules['username'] = "required|min_length[5]|max_length[12]|callback_check_username";
$rules['password'] = "required|matches[passconf]";
$rules['passconf'] = "required";
$rules['email'] = "required|valid_email||callback_check_email";
$this->validation->set_rules($rules);
$check_rules = $this->validation->run();
if ( ($check_rules == false) )
{
$this->load->view('register_new.php');
}
else
{
$data['username'] = $this->input->post ('username');
$data['password'] = $this->input->post ('password');
$data['email'] = $this->input->post ('email');
$status = $this->register_process( $data );
$status_info = $status['status'];
if ( $status_info == 'Success' )
{
$fields['username'] = 'Username';
$fields['password'] = 'Password';
$fields['passconf'] = 'Password Confirmation';
$fields['email'] = 'Email Address';
$this->validation->set_fields($fields);
$this->load->view('register_success.php');
}
elseif ( $status_info == 'Error' )
{
}
}
}
function check_username( $data )
{
if ( $this->User_model->User_Check( $data, 'username') == true )
{
$this->validation->set_message('check_username', "The Username: '$data' is allready taken, please chose another one.");
return FALSE;
}
else
{
return TRUE;
}
}
function check_email( $data )
{
if ( $this->User_model->User_Check( $data, 'email') == true )
{
$this->validation->set_message('check_email', "The Email address '$data' has been used to sign-up before. ");
return FALSE;
}
else
{
return TRUE;
}
}
}
?>
Does anyone have a solution?
[eluser]Matthieu Fauveau[/eluser]
Hi herrkaleun,
It's normal behavior, $this->validation->username is only set when validation failed.
You could simply do that to avoid getting the notice :
Code: <input type="text" name="username" value="<?= ($this->validation->username!='') ? $this->validation->username : '' ?>" size="50" />
PS : There is no need to have $this->load->library('validation') in your view file.
[eluser]The Wizard[/eluser]
hmm thanks, but in the manual its i think, described a little bit different.
thank you very very much
[eluser]The Wizard[/eluser]
ive found out, that the setted fields arent triggered because they are in the wrong place in the controller
i will post the fixed code later on
[eluser]The Wizard[/eluser]
Hello friends
ive fixed the code, the propper one looks like:
Code: <?php
class User extends Controller
{
function __construct()
{
parent::Controller();
$this->load->library( 'session' );
$this->load->library( 'parser' ); /* load template parser */
$this->load->model ( 'User_model' );
$this->load->database();
}
function register_process( $data )
{
/* add additional data, or form the data like you want. */
$data['status'] = "Pending";
$data['activation'] = 31337 + rand( 0, 666666999);
$this->load->helper('date');
$data['datetime'] = mdate( "%Y-%m-%d %H:%i:%s" );
$result_register = $this->User_model->User_Register( $data );
return $result_register;
}
function register ()
{
$this->load->helper( 'form' );
$this->load->helper( 'url' );
$this->load->library('validation');
/* by default the error messages are displayed in <p> tags. */
$this->validation->set_error_delimiters('<div class="error">', '</div>');
//$rules['username'] = "callback_database_check";
$rules['username'] = "required|min_length[5]|max_length[12]|callback_check_username";
$rules['password'] = "required|matches[passconf]";
$rules['passconf'] = "required";
$rules['email'] = "required|valid_email||callback_check_email";
$this->validation->set_rules($rules);
$check_rules = $this->validation->run();
if ( ($check_rules == false) )
{
$fields['username'] = 'Username';
$fields['password'] = 'Password';
$fields['passconf'] = 'Password Confirmation';
$fields['email'] = 'Email Address';
$this->validation->set_fields($fields);
$this->load->view('register_new.php');
}
else
{
$data['username'] = $this->input->post ('username');
$data['password'] = $this->input->post ('password');
$data['email'] = $this->input->post ('email');
$status = $this->register_process( $data );
$status_info = $status['status'];
if ( $status_info == 'Success' )
{
$this->load->view('register_success.php');
}
elseif ( $status_info == 'Error' )
{
}
}
}
function check_username( $data )
{
if ( $this->User_model->User_Check( $data, 'username') == true )
{
$this->validation->set_message('check_username', "The Username: '$data' is allready taken, please chose another one.");
return FALSE;
}
else
{
return TRUE;
}
}
function check_email( $data )
{
if ( $this->User_model->User_Check( $data, 'email') == true )
{
$this->validation->set_message('check_email', "The Email address '$data' has been used to sign-up before. ");
return FALSE;
}
else
{
return TRUE;
}
}
}
?>
the model:
Code: <?php
/*
WARNING!
Fields must be XSS filtered BEFORE calling this routine.
They should have been filtered IN the controller.
*/
class User_model extends Model {
function User_model()
{
parent::Model();
$this->load->database();
}
function User_Check ( $data, $type )
{
if ( $data == '' )
{
return false;
exit ();
}
$this->db->select ('id');
$this->db->from ('users');
if ( $type == 'email')
{
$this->db->where ( 'email', $data );
}
elseif ( $type == 'username')
{
$this->db->where ( 'username', $data );
}
$query = $this->db->count_all_results();
if ($query > 0)
{
/* email exists */
return true;
}
else
{
return false;
}
}
function UTEST ()
{
return false;
}
function User_Register( $data )
{
$this->db->insert('users', $data);
return array(
"status" => "Success",
"error_code" => "0"
);
}
}
?>
[eluser]The Wizard[/eluser]
Code: <?php
?>
<html>
<head>
<title>Register</title>
</head>
<body>
<?php echo form_open('user/register'); ?>
<h5>Username</h5>
<?
echo $this->validation->username_error;
$username = array(
'name' => 'username',
'id' => 'username',
'value' => $this->validation->username,
'size' => '50',
'class' => 'input',
);
echo form_input( $username );
?>
<h5>Password</h5>
<?
echo $this->validation->password_error;
$password = array(
'name' => 'password',
'id' => 'password',
'value' => $this->validation->password,
'size' => '50',
'class' => 'input',
);
echo form_input( $password );
?>
<h5>Password Confirm</h5>
<?
echo $this->validation->passconf_error;
$passconf = array(
'name' => 'passconf',
'id' => 'passconf',
'value' => $this->validation->passconf,
'size' => '50',
'class' => 'input',
);
echo form_input( $passconf );
?>
<h5>Email Address</h5>
<?
echo $this->validation->email_error;
$email = array(
'name' => 'email',
'id' => 'email',
'value' => $this->validation->email,
'size' => '50',
'class' => 'input',
);
echo form_input( $email );
?>
<div><input type="submit" value="Submit" /></div>
</form>
</body>
</html>
[eluser]dobbler[/eluser]
Thanks for this, it really helped me out.
[eluser]The Wizard[/eluser]
not a big deal bro !
this community rocks, everyone helps !
|