1

I am not new to CI, but trying something different and moving my validations from my controller (there's lots and its getting messy) to the form_validation.php file in the /application/config directory.\

The method I am trying to use is the function based on the controller/method where it should auto-load the rules based on where you run $this->form_validation->run()

I have read the documentation (many times) and I have seen other posts on stackoverflow and none have given me a solution...

my current setup is below...

application/config/form_validation.php

//I know the file is being loaded as these work
$config['error_prefix'] = '<span class="text-danger">';
$config['error_suffix'] = '</span>';

/**
 * METHOD SPECIFIC VALIDATIONS
 */

/* Controller:  Account
 * Method:      Register
 */
$config = array(
'account/register' => array(
        'field' => 'company',
        'label' => 'Company',
        'rules' => 'required|is_unique[company.companyName]',
        array(
            'required' => 'You have not provided {field}.',
            'is_unique' => 'This {field} already exists.'
        )
    ),
    array(
        'field' => 'username',
        'label' => 'Username',
        'rules' => 'alpha_numeric|trim|required|is_unique[users.username]',
        array(
            'required' => 'You have not provided {field}.',
            'is_unique' => 'This {field} already exists.'
        )
    ),
    array(
        'field' => 'firstname',
        'label' => 'First Name',
        'rules' => 'required'
    ),
    array(
        'field' => 'lastname',
        'label' => 'Last Name',
        'rules' => 'required'
    ),
    array(
        'field' => 'password',
        'label' => 'Password',
        'rules' => 'required|min_length[6]',
        array(
            'min_length' => '{field} must have at least {param} characters.'
        )
    ),
    array(
        'field' => 'passconf',
        'label' => 'Confirm Password',
        'rules' => 'required|matches[password]'
    ),
    array(
        'field' => 'email',
        'label' => 'Email',
        'rules' => 'trim|required|valid_email|is_unique[users.email]',
        array(
            'required' => 'You have not provided {field}.',
            'is_unique' => 'This {field} already exists.'
        )
    )
);

Controller:

class Account extends CI_Controller
{

public function __construct()
{
    parent::__construct();
    $this->load->helper('form');
    $this->load->library('form_validation');
    $this->load->model('account_model');
}

public function register()
{
            //Form not yet submitted, user not logged in, display login page
    if ($this->form_validation->run() == FALSE and $this->session->userdata('loginuser') == FALSE) {

        $this->load->view('templates/header');
        $this->load->view('account/register');
        $this->load->view('templates/loadjs');

    } else {
}

View Snippet:

<input class="form-control" name="company" placeholder="Company Name" type="text" value="<?php echo set_value('company'); ?>" autofocus />
</div>
<div><?php echo form_error('company'); ?></div>

Going by the documentation I linked, you should be able to just use $this->form_validation->run() and it will auto-call these rules?

8
  • For clarity, can you state your controller and method name? Is it in fact a method named register in a controller named account? Can you show the code where you are calling the validation, and perhaps the html snippet of the form as well? The reason I ask is because on the surface of your description it sounds like you are doing it correctly, but as it is not working, something, somewhere must be incorrect. Commented May 29, 2016 at 1:44
  • I have done this, please note if I call the validations the normal inline $this->form_validation->set_rules('company', 'Company', 'required|is_unique[company.companyName]'method in the controller/view still works perfectly. Thanks Commented May 29, 2016 at 1:49
  • I notice that in your given examples when you call it directly you do not use the error message array that is present in your form_validation.php. Have you tried your form_validation.php file without the defined error message arrays, or conversely, are you also having success using set_message but it is not in your example? Commented May 29, 2016 at 2:03
  • Sorry, didn't copy that bit, I am using the set error messages in the array directly in the controller and they are working... It's very strange Commented May 29, 2016 at 2:09
  • Have you tried removing the session data part of the conditional just to rule out that as the reason the validation does not run? Commented May 29, 2016 at 16:24

2 Answers 2

1

For those playing at home the answer was that I was over-writing the $config array with my original method. As the validation file is included and not separate, it assigns rather than appends the $config array.

$config['account/register'] = array(
    array(
        'field' => 'company',
        'label' => 'Company',
        'rules' => 'required|is_unique[company.name]',
        'errors' => array(
            'required' => 'You have not provided {field}.',
            'is_unique' => 'This {field} already exists.'
        )
    ),
    array(
        'field' => 'username',
        'label' => 'Username',
        'rules' => 'alpha_numeric|trim|required|is_unique[users.username]',
        'errors' => array(
            'required' => 'You have not provided {field}.',
            'is_unique' => 'This {field} already exists.'
        )
    ),
    array(
        'field' => 'firstname',
        'label' => 'First Name',
        'rules' => 'required'
    ),
    array(
        'field' => 'lastname',
        'label' => 'Last Name',
        'rules' => 'required'
    ),
    array(
        'field' => 'password',
        'label' => 'Password',
        'rules' => 'required|min_length[5]',
        'errors' => array(
            'min_length' => '{field} must have at least {param} characters.'
        )
    ),
    array(
        'field' => 'passconf',
        'label' => 'Confirm Password',
        'rules' => 'required|matches[password]'
    ),
    array(
        'field' => 'email',
        'label' => 'Email',
        'rules' => 'trim|required|valid_email|is_unique[users.email]',
        'errors' => array(
            'required' => 'You have not provided {field}.',
            'is_unique' => 'This {field} already exists.'
        )
    )
);
Sign up to request clarification or add additional context in comments.

Comments

0

Looking at your code again, I think I see a mismatch. I see that you are loading the form_validation as a library, however that should mean there is something in the library folder you are not showing us, or maybe you meant to use a config class instead to access it since it is in the config folder. If that is true you might want to look at the config class.

1 Comment

The issue is right there in the code I wrote all that time ago $config = array() this is overwriting the whole $config array. I'll add an answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.