CodeIgniter验证未在表单上显示

时间:2018-05-06 09:19:44

标签: php codeigniter validation codeigniter-3

此处有另一个CI Validation错误。我试过搜索,从我能看到的,我的代码是好的。验证运行 - 如果我只是从控制器回显“Validation Failed”字符串,它会显示。

但我似乎无法在实际视图中显示它。即使我在视图中有一行(即echo validation_errors();),也没有错误输出,即使它失败validation

任何指针都将非常感激:)

控制器

public function add() {
    if ($this->form_validation->run('user_add_edit') == FALSE)
    {
        //Validation failed
        $this->load->view('templates/header_generic');
        $this->load->view('templates/navigation');
        $this->load->view('user/add_user_form');
        $this->load->view('templates/footer_generic');
    }
    else
    {
        echo "Form validated!";
    }
}

查看(部分)

                        <div class="panel-body">

                        <?php echo validation_errors(); ?>

                        <?php echo form_open('user/add'); ?>
                            <label for="email">
                                Email Address
                            </label><br />
                            <div class="form-group input-group <?php echo null === form_error('email') || is_null(form_error('email')) ? 'form-group has-error' : ''; ?>">
                                <span class="input-group-addon">@</span>
                                <?php echo form_input($email_attr, set_value('email')); ?>
                            </div>
                            <br />
                            <?php echo form_error('email');?>
                            <br />
                            <?php echo form_fieldset("Password"); ?>
                            jfkdjflkdjflks
                            <?php echo form_fieldset_close(); ?>
                            <br />
                            <?php echo form_submit("submit", "Add New User", "class='btn btn-success'"); ?>
                        </form>
                    </div>

表单验证

$config = array(
'user_add_edit' => array(
    array(
        'field' => 'email',
        'label' => 'Email Address',
        'rules' => 'trim|required|valid_email|is_unique[user.email]',
        'errors'    => array(
            'required'  => 'You must enter a %s',
            'valid_email'   => '%s is not a valid email address',
            'is_unique'     => 'This email address already exists'
        )
    ),

1 个答案:

答案 0 :(得分:2)

MY_Form_validation.php设置不正确可能会导致通过配置文件设置表单规则。

<强>修正

application/libraries/MY_Form_validation.php中 - 使用以下代码替换您的构造函数,或者只需添加$ config参数即可按照以下更改操作。

function __construct($config = array()){
    parent::__construct($config);
    $this->CI =& get_instance();
}

可能性,$ config变量的值被覆盖application/config/form_validation.php内发生的值。检查一下。

<强>替代:

从控制器方法加载form_validation.php配置文件,并将相关配置项传递给set_rules(..),如下所示。

public function add() {
    $this->load->config('form_validation');
    $this->form_validation->set_rules($this->config->item('user_add_edit'));

    if ($this->form_validation->run() == FALSE)
    {
        //Validation failed
        $this->load->view('templates/header_generic');
        $this->load->view('templates/navigation');
        $this->load->view('user/add_user_form');
        $this->load->view('templates/footer_generic');
    }
    else
    {
        echo "Form validated!";
    }
}