2

我在控制器中设置了一个表单,它从数据库加载表单及其先前填充的内容,并根据需要处理表单。问题是 $this->form_validation->run() 永远不会评估为 FALSE,即使不符合规则。

控制器:

public function edit_version($node, $lang)
{
    // load form validation class
    $this->load->library('form_validation');

    // set validation rules
    $this->form_validation->set_rules("title|Title|required");

    // run validation
    if ($this->form_validation->run() !== FALSE)
    {
        // save input to database
        die("validation successful");
    }
    else
    {
        // either validation was not passed or no data to validate
        // load page edition view and display databse contents

        // load page model
        $this->load->model("page_model");

        // get the page from database
        $data["page"] = $this->page_model->get_page($node, $lang);

        // load views
        $this->load->view("admin/header.php", array("title" => "Edit page no.: $node, $lang version - Admin"));
        $this->load->view("admin/pages/edit_page.php", $data);
        $this->load->view("admin/footer.php");
    }
}

模型:

class Page_model extends CI_Model
{
public function get_page($node, $lang)
{
    // load the page
    return $this->db->get_where("pages", array("node" => $node, "lang" => $lang))->row();
}

public function save_version($page)
{
    $this->db->where("node", $page["node"]);
    $this->db->where("lang", $page["lang"]);
    $this->db->update("pages", $page);
}

public function search($query)
{
    return $this->db->get_where("pages", $query)->result();
}
}

看法:

<h2>Edit page</h2>
<?php
// load form helper
    $this->load->helper("form");

// open a form
echo form_open("admin/page/{$page->node}/edit/{$page->lang}");

// print validation errors
echo validation_errors();

// title and content fields
echo form_label("Title: ", "title");
echo form_input("title", set_value("title", $page->title));

// aesthetic line break
echo "<br>";

echo form_label("Content: ", "content") . "<br>";
echo form_textarea("content", set_value("content", $page->content));

// save button and close form
echo form_submit("submit", "Save page");
echo form_close();

提前致谢。

4

3 回答 3

4

设置规则的语法是

 $this->form_validation->set_rules('field_name', 'Label', 'rule1|rule2|rule3');

通过考虑规则,您设定的规则线将是

$this->form_validation->set_rules('title','Title', 'required");
于 2012-11-30T19:41:05.097 回答
2

我不是 CodeIgniter 经验丰富的开发人员,但从文档来看,正确的语法不是以下内容吗?

$this->form_validation->set_rules("title","Title","required");

根据此链接: http ://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#validationrules

于 2012-11-30T14:43:52.243 回答
0

利用

$this->form_validation->set_rules("title","Title","required");

而不是这个

$this->form_validation->set_rules('title','Title','required');

我已经对此进行了测试。它就像一个魅力。

于 2017-04-15T22:58:15.417 回答