CodeIgniter Form Validation Not Working

12,658

Solution 1

I have FINALLY found the answer! My server wasn't configured correctly, I didn't have mod_rewrite enabled so none of the requests were working properly or something.

Ubuntu users, fire this off on the command line to enable it: sudo a2enmod rewrite

Then restart Apache.

I am using the following .htaccess:

<IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteBase /

    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #When your application folder isn't in the system folder
    #This snippet prevents user access to the application folder
    #Rename 'application' to your applications folder name.
    RewriteCond %{REQUEST_URI} ^application.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php?/$1 [L]

</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    ErrorDocument 404 /index.php
</IfModule>

Solution 2

You didn't give the controller name in the form_open function, it should be

<?php  echo form_open('registerController/register'); ?>

Documentation: Form Helper.

Share:
12,658
Adam Waite
Author by

Adam Waite

I make iOS apps in East London. I make web apps occasionally too, mainly with Rails and JS.

Updated on June 05, 2022

Comments

  • Adam Waite
    Adam Waite almost 2 years

    Can't seem to get form validation working on code igniter, unsure what's wrong!

    Here's my controller class:

    class registerController extends MY_Controller {
    
        // --- Methods -----------------
    
        function __construct()
        {
            parent::__construct();
    
            $this->firephp->log('REGISTER PAGE CONTROLLER ACTIVE');
    
            //Load user model
            $this->load->model('usersModel');
    
            //load form validation
            $this->load->helper('form');
            $this->load->library('form_validation');
    
        }
    
        //------------------------------
    
        public function register()
        {
    
            $this->form_validation->set_rules('registerForenameFieldName', 'Fist Name', 'required');
    
            //This is executed when the form is submitted
            if ($this->form_validation->run() == FALSE)
            {
    
                $this->firephp->log('Registration form validation failed');
    
                //Load in the views
                $this->load->view('global/head.php');
                $this->load->view('global/top.php');
                $this->load->view('register/register.php');
                $this->load->view('global/footer.php');
    
            } else {
    
                $this->firephp->log('Registration form validation succeeded');
    
                //Model method here
    
                //Load in the views
                $this->load->view('global/head.php');
                $this->load->view('global/top.php');
                $this->load->view('home.php');
                $this->load->view('global/footer.php');
            }
    
        }
    
    }
    

    and my form:

    <?php  echo form_open('register'); ?>
    
        <div id="registerErrors"><?php echo validation_errors(); ?></div>
    
            <table id="registerTable">
    
                <tr>
                <td class="regLeftCell"><p>First Name</p></td>
                <td class="regRightCell">
                <input id="registerForenameField" class="textField registerField" name="registerForenameFieldName" type="text" placeholder="Forename" value="<?php echo set_value('registerForenameFieldName'); ?>"></input>
                </td>
                </tr>
    
                <tr>
                <td class="regLeftCell"><p>Last Name</p></td>
                <td class="regRightCell">
                <input id="registerSurnameField" class="textField registerField" name="registerSurnameFieldName" type="text" placeholder="Surname" value="<?php echo set_value('registerSurnameFieldName'); ?>"></input>
                </td>
                </tr>
    
                <tr>
                <td class="regLeftCell"><p>Email Address</p></td>
                <td class="regRightCell">
                <input id="registerEmailField" class="textField registerField" name="registerEmailFieldName" type="text" placeholder="Email Address"></input>
                </td>
                </tr>
    
                <tr>
                <td class="regLeftCell"><p>Choose Password</p></td>
                <td class="regRightCell">
                <input id="registerPasswordField" class="textField registerField" name="registerPasswordFieldName" type="text" placeholder="Password"></input>
                </td>
                </tr>
    
                <tr>
                <td class="regLeftCell"><p>Confirm Password</p></td>
                <td class="regRightCell">
                <input id="registerConfirmPasswordField" class="textField registerField" name="registerConfirmPasswordFieldName" type="text" placeholder="Confirm Password"></input>
                </td>
                </tr>
    
                <tr>
                <td class="regLeftCell"><p>Address Line 1</p></td>
                <td class="regRightCell">
                <input id="registerAddress1Field" class="textField registerField" name="registerAddress1FieldName" type="text" placeholder="Address Line 1"></input>
                </td>
                </tr>
    
                <tr>
                <td class="regLeftCell"><p>Address Line 2</p></td>
                <td class="regRightCell">
                <input id="registerAddress2Field" class="textField registerField" name="registerAddress2FieldName" type="text" placeholder="Address Line 2"></input>
                </td>
                </tr>
    
                <tr>
                <td class="regLeftCell"><p>Address Line 3</p></td>
                <td class="regRightCell">
                <input id="registerAddress3Field" class="textField registerField" name="registerAddress3FieldName" type="text" placeholder="Address Line 3"></input>
                </td>
                </tr>
    
                <tr>
                <td class="regLeftCell"><p>Post Code</p></td>
                <td class="regRightCell">
                <input id="registerAddressPostCodeField" class="textField registerField" name="registerPostCodeFieldName" type="text" placeholder="Post Code"></input>
                </td>
                </tr>
    
                <tr>
                <td class="regLeftCell"><p>Country</p></td>
                <td class="regRightCell">
                <input id="registerAddressCountryField" class="textField registerField" name="registerSurnameFieldName" type="text" placeholder="Country"></input>
                </td>
                </tr>
    
            </table>
    
            <div id="registerButton">
                <input class="button registerSubmitButton" type="submit" value="Register"/>
            </div>
    
    
        </form>
    

    The routing is set up like this:

    $route['register'] = 'registerController/register';
    

    What's wrong? It just fails every time without displaying an error.

    It just reloads the same page with no validation errors. The $this->form_validation->run() evaluates at FALSE every time because I have logged it in FirePHP, a Firebug extension.

    EDIT:

    It may also be worth mentioning that I get a 404 error in the console despite it loading the page:

    other error