codeigniter 3配置文件中的控制器/方法验证

时间:2016-05-29 01:37:25

标签: php codeigniter

我不是CI的新手,但是尝试不同的东西并将我的验证从我的控制器(那里有很多并且变得混乱)转移到form_validation.php目录中的/application/config文件。 \

我尝试使用的方法是基于控制器/方法的功能,它应根据您运行的位置自动加载规则$this->form_validation->run()

我已阅读documentation(很多次),我在stackoverflow上看过其他帖子,没有人给我一个解决方案......

我目前的设置如下......

应用/配置/ 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.'
        )
    )
);

控制器:

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 {
}

查看代码段:

<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>

根据我链接的文档,您应该只能使用$this->form_validation->run(),它会自动调用这些规则吗?

2 个答案:

答案 0 :(得分:0)

再次查看您的代码,我认为我看到了不匹配。我看到你正在将form_validation加载为库,但是这应该意味着你没有向我们展示库文件夹中的某些东西,或者你可能想要使用配置类来访问它,因为它在config文件夹中。如果确实如此,您可能需要查看documentation

答案 1 :(得分:0)

对于那些在家里玩的人来说,答案是我用原始方法覆盖了$ config数组。由于验证文件是包含的而不是单独的,它分配而不是附加$ config数组。

$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.'
        )
    )
);