CodeIgniter的表单验证问题

时间:2011-06-08 19:55:59

标签: forms codeigniter validation

我有这个观点:

   <?php echo form_open(); ?>

<?php echo form_input('username', '', ''); ?>

<?php echo form_submit('submit', 'Submit'); ?>

<?php echo form_close(); ?>

<?php echo validation_errors(); ?>

和这个控制器方法:

function test() {


        $username = $this->input->post('username');

        $this->form_validation->set_rules($username, 'Username', 'required|min_length[4]');

        if ($this->form_validation->run() !== FALSE) {

            echo 'Tada!';

        }

        $this->load->view('test');        

    }

但是当我将用户名字段留空时,没有任何反应。但是,如果我在其中键入内容,则会告诉我该字段是必需的。我已经下载并给CI一个新安装了近十次,尝试加载有没有不同的帮助等等。它变得非常令人沮丧,请帮忙。

3 个答案:

答案 0 :(得分:1)

尝试使用:

$this->form_validation->set_rules('username', 'Username', 'required|min_length[4]');

答案 1 :(得分:1)

问题在于set_rules的第一个参数,即字段名称。

您传递的 $ username 基本上是设置字段名称以验证用户输入字段中的内容。如果您在输入框中输入“username”,您会看到表单已经过验证。

更改行

$this->form_validation->set_rules($username, 'Username', 'required|min_length[4]');

$this->form_validation->set_rules('username', 'Username', 'required|min_length[4]');

答案 2 :(得分:1)

可能是这一行的问题

<?php echo form_open(); ?>

如果将其留空,它基本上会发送回控制器本身并仅调用构造和索引函数。在这种情况下,处理表单处理的函数是“test()”

试试这个

<?php echo form_open('yourControllerName/test'); ?> //test is the function dealing with

如果它不起作用,试试这个

<?php echo form_open('test'); ?>
相关问题