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?
$this->form_validation->set_rules('company', 'Company', 'required|is_unique[company.companyName]'method in the controller/view still works perfectly. Thanks