Codeigniter表单验证类

时间:2010-07-05 22:33:10

标签: php codeigniter validation

我有一个有趣的事情继续我的本地开发设置。使用codeigniter我有一个表单应该模拟从数据库中的用户存储一些信息。此时我还没有数据库设置,我只希望表单在验证完成后将我发送到成功页面。但是当我提交表单时,它会返回并告诉我名称字段为空,我从未获得成功页面。我错过了什么吗?请仔细看看,告诉我是否遗漏了什么! 这是控制器中处理表单的函数。

function showsupport()
{
    $this->form_validation->set_rules('supportername','Name','trim|required|max_length[30]');
    $this->form_validation->set_rules('supporteremail','Email Address','trim|required|valid_email');
    $this->form_validation->set_rules('pledgedate','Pledge Date','trim|required');
    $this->form_validation->set_rules('messagetxt','Your Message','trim|required|xss_clean');

    if($this->form_validation->run() == FALSE)
    {
        $this->template->write_view('content','supportus');
        $this->template->render();
    } else {
        $this->template->write('content','Your submission was a success');
        $this->template->render();
    }
}


<div id="supportform" class="formblock">

                <?php
                  $formdata = array('id'=>'suppform');
                      echo form_open('homepage/showsupport',$formdata);
                     $namedata = array('name'=>'supportname','id'=>'supportname','size'=>'30','max_length'=>'25');
                     echo '<label for="supportername">Your Name:'.form_input($namedata,set_value('supportname')).'</label><br /><br />';
                     $emaildata =  array('name'=>'supporteremail','id'=>'supporteremail','size'=>'30','max_lenth'=>'25');
                     echo '<label for="supporteremail">Email Address:'.form_input($emaildata,set_value('suppoteremail')).'</label><br /><br />';
                     $pledgedata = array('name'=>'pledgedate','id'=>'pledgedate','size'=>'30','max_length'=>'20');
                     echo '<label for="pledgedate">Today\'s Date:'.form_input($pledgedata,set_value('pledgedate')).'</label><br /><br />';
                     $msgdata = array('name'=>'messagetxt','id'=>'messagetxt','col'=>'2','rows'=>'8');
                     echo '<label for="messagetext">Your Pledge:'.form_textarea($msgdata).'</label><br />';
                     $submitdata = array('name'=>'submitbtn','id'=>'submitbtn','value'=>'Send');
                     echo '<label for="submitbutton">'.form_submit($submitdata).'</label><br />';
                     echo form_close();
                ?>
            </div>
        <div id="errorsechoed">
            <div class="ui-widget">
        <div class="ui-state-error ui-corner-all" style="padding: 0 .7em; border: none;">                        
                      <?php echo validation_errors('<p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span>');?>

这是它自己的形式。我必须在几个小时内做一个演示,这种形式的工作是关键。谢谢你们的帮助。

电子

1 个答案:

答案 0 :(得分:2)

表单验证规则与视图html元素之间存在名称不匹配。

验证规则

$this->form_validation->set_rules('supportername','Name','trim|required|max_length[30]');

HTML

$namedata = array('name'=>'supportname','id'=>'supportname','size'=>'30','max_length'=>'25');
 echo '<label for="supportername">Your Name:'.form_input($namedata,set_value('supportname')).'</label><br /><br />';

将验证规则名称更改为“supportname”

更改'name'=&gt; 'supportname'到'name'=&gt; 'supportername'