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

#1
[eluser]gedev2006[/eluser]
Hi

Can someone give me a pointer on this function, its not appearing to output anything in the view file! I have a partially completed controller as follows (appologies if the browser has messed up the structure of the code pasted from Dreamweaver, you should get the correct view by pasting it back):

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-0]%'; /// 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]markup2go[/eluser]
I didn't see your view file's code but after a quick glance I believe you have to define the array keys for your rules when you pass them to $this->form_validation->set_rules();

Also I'm assuming your index() function loads the view which contains your error outputs such as <?php echo form_error('fieldname'); ?> ?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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