0

所以我第一次尝试使用 CI 内置的 form_validation “工具”来获得很大的帮助,而研究我似乎无法掌握它。我的视图设置正确,我的控制器似乎正确...我的问题是当我单击“提交”按钮时,它链接到新功能,但无论我输入什么都不显示任何错误消息“名称”、“电子邮件”框。我希望它现在几乎在任何地方都显示错误消息,我只需要它运行验证并显示错误消息。

控制器文件

public function intelli()
{
    $data["messages"] = "";
    $this->load->view('engage_header');
    $this->load->view('engage_nav');
    $this->load->view('intelli/content_intelli', $data);
    $this->load->view('engage_footer');

}
public function intelli_email()
{
    $this->load->helper('form', 'url'));
    $this->load->library('form_validation');

    $this->form_validation->set_rules('name_area', 'Name', 'required|alpha|xss_clean');
    $this->form_validation->set_rules('email_area', 'Email', 'required|valid_email|xss_clean');
    $this->form_validation->set_rules('phone_area', 'Phone', 'is_numeric|xss_clean');

    if ($this->form_validation->run() == FALSE) {
        $data["messages"] = "";
    } else {

        $data["messages"] = "The Email has successfully been sent";
    }

    $this->load->view('engage_header');
    $this->load->view('engage_nav');
    $this->load->view('intelli/content_intelli', $data);
    $this->load->view('engage_footer');
}

查看文件

<?php 
    $this->load->helper("form");
?>
<div class="request_demo">
    <div id="request_logo"></div>
    <div id="top_paragraph">
    Intelliship & HALO deliver immediate and quantifiable ROI.  Schedule a demo to learn how this transportation management software will positively impact your company&apos;s bottom-line profit.
    </div>
    <div id="entry_box">
    <?php  echo validation_errors();
    echo $messages;
    ?>
    <div id="request_form">
    <?php echo form_open('/index.php/site/intelli_email')?>
            <input id="name_area" type="text" placeholder="NAME" value="" required/><br/>
            <input id="email_area" type="text" placeholder="EMAIL" value="" required/><br/>
            <input id="phone_area" type="text" placeholder="PHONE" value=""><br/>
            <input id="submit_req" type="submit" value="SUBMIT">
        </form> 
    </div>
</div>

我知道代码现在很草率,但我只是在寻找功能而不是美观。还注意到,因为我在控制器中加载了 helper('form'),所以在视图中加载它是多余的。

4

2 回答 2

2

您只id为表单元素指定。name也给他们

<input id="name_area" name ="name_area" type="text" placeholder="NAME" value="" required/><br/>
<input id="email_area" name="email_area" type="text" placeholder="EMAIL" value="" required/><br/>
<input id="phone_area" name="phone_area" type="text" placeholder="PHONE" value=""><br/>

Codeigniter 连接规则集,元素基于名称,而不是 id

于 2012-08-23T22:29:31.630 回答
0

或者你可以像这样给

<input type="text" name="name_area" value="">
<?php echo form_error('name_area'); ?>

对于不同的字段,forn_error 值会发生变化,例如 email_area,phone_area.... 它显示与字段名相对应的错误(验证)。

于 2012-08-24T05:50:44.300 回答