• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
validation_errors()

#1
[eluser]gedev2006[/eluser]
Can someone please advise me as to why the validation_errors() is outputting nothing at all. Here is my controller code ( unfinished and pasted from Dreamweaver )

Code:
<?php

class Register extends Controller {

    public $css = array ( 'register' );


    /*
        function: __construct()
            load up required helpers, libraried and models
    */
    public function Register() {
    
        parent::Controller();
            
        // Load form helper    
        $this->load->helper ( 'form' );

        // Load up form helper and validation libraru
        $this->load->library ( 'form_validation' );
        
        // Load users model
        $this->load->model ( 'users_model' );
            
        
    }
        
    /*
        function: index
            simply show the registration form
    */
    public function index() {
    
        // Set page title
        $this->siteSettings->page = 'Register For An Account';
        
        // Create page
        $_pageContent = $this->load->view ( 'register/register', array(), true );
        
        // Show layout
        $this->load->view ( 'layouts/main', array ( 'page_content' => $_pageContent ) );
        
    }
    
    
    /*
        function: process
            process a submitted register form.
            must include the hidden field "register" with value set to "1"
    */
    public function process() {
    
        // Check the register form has been sent
        if ( $this->input->post ( 'register' ) && $this->input->post ( 'register' ) == '1' ) {
        
            // Set validation rules
            $_rules = array (
                        array ( 'username', 'Username', 'required|callback_unique_username' ),
                        array ( 'password', 'Password', 'required' ),
                        array ( 'verifypassword', 'Verify Password', 'required|matches[password]' ),
                        array ( 'firstname', 'First Name', 'required' ),
                        array ( 'lastname', 'Last Name', 'required' ),
                        array ( 'email', 'Email', 'required|valid_email' ),
                        array ( 'dob', 'Date Of Birth', 'required|callback_valid_dob' )
                        );
                        
            $this->form_validation->set_rules ( $_rules );
            
            
            if ( $this->form_validation->run() ) {
            
                die ("TESTING FORM VALIDATION SUCCESS" );
                
                // Perform registration
                $this->users_model->registerUser (
                    $this->input->post ( 'username' ),
                    $this->input->post ( 'password' ),
                    $this->input->post ( 'firstname' ),
                    $this->input->post ( 'lastname' ),
                    $this->input->post ( 'email' ),
                    $this->input->post ( 'dob' )
                     );
                    
                // Show register thanks page
                $this->load->view ( 'layout/main', array ( 'page_content' => $this->load->view ( 'register/thanks', array(), true ) ) );
                
            } else {
            
                // invalid fields
                $this->index();
                
            }
            
        } else {
        
            $this->index();
            
        }
        
    }
    
        /*
            validation callback functions
        */
        
        
        /*
            function unique_username
                check the username against the database
        */
        public function unique_username ( $username ) {
        
            if ( $_user = $this->users_model->getByUsername ( $username ) ) {
            
                // user already exists
                $this->form_validation->set_message ( 'unique_username', 'Username is already taken' );
            
                return false;
                
            } else {
            
                return true;
                
            }
            
        }
        
        /*
            function: valid_dob
                perform REGEX on supplied DOB to make sure the text
                entered is returned in the format of mm/dd/yyyy
        */
        public function valid_dob ( $dob ) {
        
            $_pattern = '%[0-1][1-2]/[0-3][1-1]/[1-2][9-0][0-9][0-9]%'; /// may need a little revision!
            
            if ( !preg_match ( $_pattern, $dob ) ) {
            
                $this->form_validation->set_message ( 'valid_dob', 'Date of birth invalid' );
                
                return false;
                
            } else {
            
                return true;
                
            }
            
        }
    
}

?>

#2
[eluser]gedev2006[/eluser]
View file code

Code:
<div id="innerContent">
                
    <div class="contentContainer">
    
        <div class="contentHeader"><h1>&lt;?= $this->siteSettings->pageTitle(); ?&gt;</h1></div>
        
        <div class="content">
        
            <div class="errors">&lt;?= validation_errors(); ?&gt;</div>

            &lt;?= $this->blocks_model->getByIdentifier ( 'registerMessage' )->getContent(); ?&gt;
                        
            &lt;form action="&lt;?= $this-&gt;siteSettings->getLink ( 'register/process' ); ?&gt;" method="post">
                <fieldset>
                    <table>
                        <tr>
                            <th>Username: <span class="required">*</span></th>
                            <td>&lt;input type="text" name="username" /&gt;&lt;/td>
                        </tr>
                        <tr>
                            <th>Password: <span class="required">*</span></th>
                            <td>&lt;input type="password" name="password" /&gt;&lt;/td>
                        </tr>
                        <tr>
                            <th>Re-type password: <span class="required">*</span></th>
                             <td>&lt;input type="password" name="verifypassword" /&gt;&lt;/td>
                        </tr>
                        <tr>
                            <th>First name: <span class="required">*</span></th>
                            <td>&lt;input type="text" name="firstname" /&gt;&lt;/td>
                        </tr>
                        <tr>
                            <th>Last name: <span class="required">*</span></th>
                            <td>&lt;input type="text" name="lastname" /&gt;&lt;/td>
                        </tr>
                        <tr>
                            <th>Email: <span class="required">*</span></th>
                            <td>&lt;input type="text" name="email" /&gt;&lt;/td>
                        </tr>
                        <tr>
                            <th>Date of birth<br />(mm/dd/yyyy): <span class="required">*</span></th>
                            <td>&lt;input type="text" name="dob" /&gt;&lt;/td>
                        </tr>
                        <tr>
                            <th>Website: </th>
                            <td>&lt;input type="text" name="website" /&gt;&lt;/td>
                        </tr>
                        <tr>
                            <th>MSN: </th>
                            <td>&lt;input type="text" name="msn" /&gt;&lt;/td>
                        </tr>
                        <tr>
                            <th>AIM: </th>
                            <td>&lt;input type="text" name="aim" /&gt;&lt;/td>
                        </tr>
                        <tr>
                            <th>YIM: </th>
                            <td>&lt;input type="text" name="yim" /&gt;&lt;/td>
                        </tr>
                        <tr>
                            <th>Avatar: </th>
                            <td>&lt;input type="file" name="avatar" /&gt;&lt;/td>
                        </tr>
                        <tr><td>&nbsp;</td></tr>
                        <tr>
                            <th class="required">* required fields</th>
                            <td colspan="2" align="left">&lt;input type="submit" value="Complete registration" /&gt;&lt;/td>
                        </tr>
                        
                    </table>
                    &lt;input type="hidden" name="register" value="1" /&gt;
                </fieldset>
            &lt;/form&gt;
                                            
        </div>
            
    </div>

</div>

#3
[eluser]gedev2006[/eluser]
my fault! didnt read the documentation quite right. revised code:

Code:
// Set validation rules
            $_rules = array (
                        array ( 'field' => 'username', 'label' => 'Username', 'rules' => 'required|callback_unique_username' ),
                        array ( 'field' => 'password', 'label' => 'Password', 'rules' => 'required' ),
                        array ( 'field' => 'verifypassword', 'label' => 'Verify Password', 'rules' => 'required|matches[password]' ),
                        array ( 'field' => 'firstname', 'label' => 'First Name', 'rules' => 'required' ),
                        array ( 'field' => 'lastname', 'label' => 'Last Name', 'rules' => 'required' ),
                        array ( 'field' => 'email', 'label' => 'Email', 'rules' => 'required|valid_email' ),
                        array ( 'field' => 'dob', 'label' => 'Date Of Birth', 'rules' => 'required|callback_valid_dob' )
                        );


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.