为什么我的CodeIgniter表单验证不起作用?

时间:2010-06-13 19:31:46

标签: php codeigniter validation

我正在尝试在使用表单助手创建的简单联系表单上设置验证。根本没有验证。有什么问题?

在下面的代码中,无论输入到表单中的内容如何,​​“good”关键字始终显示,并且永远不会显示通过set_value设置的保存值。

控制器

// Contact
    function contact() {
        $data['pageTitle'] = "Contact";
        $data['bodyId'] = "contact";

        $this->load->library('form_validation');

        $config_rules = array ('email' => 'required','message' => 'required');

        $this->form_validation->set_rules($config_rules);

        if ($this->form_validation->run() == FALSE) {
            echo "bad";            
            $data['include'] = "v_contact";    
            $this->load->view('v_template',$data);

        } else {
            echo "good";
            $data['include'] = "v_contact";        
            $this->load->view('v_template',$data);
        }

    }

查看

echo validation_errors();
echo form_open('events/contact');

// name
echo form_label('Name', 'name');
$data = array (
        'name' => 'name',
        'id' => 'name',
        'maxlength' => '64',
        'size' => '40',
        'value' => set_value('name')
    );
echo form_input($data) . "\n<br />";

// email address
echo form_label('Email Address', 'email');
$data = array (
        'name' => 'email',
        'id' => 'email',
        'maxlength' => '64',
        'size' => '40',
        'value' => set_value('email')
    );
echo form_input($data) . "\n<br />";

// message
echo form_label('Message', 'message');
$data = array (
        'name' => 'message',
        'id' => 'message',
        'rows' => '8',
        'cols' => '35',
        'value' => set_value('message')
    );
echo form_textarea($data) . "\n<br />";

echo form_submit('mysubmit', 'Send Message');

echo form_close(); 

1 个答案:

答案 0 :(得分:3)

您似乎没有根据新Form_validation库的方式设置验证规则(用户指南在新语法上有section)。这似乎是旧Validation库的语法。

请尝试使用此$config_rules数组,看看您的验证是否正常运行:

$config_rules = array(
    array('field' => 'email', 'rules' => 'required'), 
    array('field' => 'message', 'rules' => 'required')
);

$this->form_validation->set_rules($config_rules);