Welcome Guest, Not a member yet? Register   Sign In
SOLVED - Validation Error while Reproducing form...
#1

[eluser]The Wizard[/eluser]
Hello FriendsSmile

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>
&lt;input type="text" name="username" value="&lt;?php echo $this-&gt;validation-&gt;username;?&gt;" size="50" />


<h5>Password</h5>
&lt;input type="text" name="password" value="" size="50" /&gt;

<h5>Password Confirm</h5>
&lt;input type="text" name="passconf" value="" size="50" /&gt;

<h5>Email Address</h5>
&lt;input type="text" name="email" value="" size="50" /&gt;

<div>&lt;input type="submit" value="Submit" /&gt;&lt;/div>

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;

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:
&lt;?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;
        }
    }

}


?&gt;


Does anyone have a solution?
#2

[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:
&lt;input type="text" name="username" value="&lt;?= ($this-&gt;validation->username!='') ? $this->validation->username : '' ?&gt;" size="50" />

PS : There is no need to have $this->load->library('validation') in your view file.
#3

[eluser]The Wizard[/eluser]
hmm thanks, but in the manual its i think, described a little bit different.

thank you very very much Smile
#4

[eluser]The Wizard[/eluser]
ive found out, that the setted fields arent triggered because they are in the wrong place in the controllerSmile
i will post the fixed code later on
#5

[eluser]The Wizard[/eluser]
Hello friendsSmile

ive fixed the code, the propper one looks like:


Code:
&lt;?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;
        }
    }

}


?&gt;

the model:
Code:
&lt;?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"
            );
    }

}


?&gt;
#6

[eluser]The Wizard[/eluser]
Code:
&lt;?php


?&gt;


&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Register&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;



&lt;?php echo form_open('user/register'); ?&gt;

<h5>Username</h5>
&lt;?
    echo $this->validation->username_error;
    $username = array(
                  'name'        => 'username',
                  'id'          => 'username',
                  'value'       => $this->validation->username,
                  'size'        => '50',
                  'class'       => 'input',
                );
    echo form_input( $username );
?&gt;

<h5>Password</h5>
&lt;?
    echo $this->validation->password_error;
    $password = array(
                  'name'        => 'password',
                  'id'          => 'password',
                  'value'       => $this->validation->password,
                  'size'        => '50',
                  'class'       => 'input',
                );
    echo form_input( $password );
?&gt;

<h5>Password Confirm</h5>
&lt;?
    echo $this->validation->passconf_error;
    $passconf = array(
                  'name'        => 'passconf',
                  'id'          => 'passconf',
                  'value'       => $this->validation->passconf,
                  'size'        => '50',
                  'class'       => 'input',
                );
    echo form_input( $passconf );
?&gt;

<h5>Email Address</h5>
&lt;?
    echo $this->validation->email_error;
    $email = array(
                  'name'        => 'email',
                  'id'          => 'email',
                  'value'       => $this->validation->email,
                  'size'        => '50',
                  'class'       => 'input',
                );
    echo form_input( $email );
?&gt;

<div>&lt;input type="submit" value="Submit" /&gt;&lt;/div>

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
#7

[eluser]dobbler[/eluser]
Thanks for this, it really helped me out.
#8

[eluser]The Wizard[/eluser]
not a big deal bro ! Smile

this community rocks, everyone helps !




Theme © iAndrew 2016 - Forum software by © MyBB