【发布时间】:2015-06-24 13:58:03
【问题描述】:
我有经典的 CI 控制器和索引方法,在该方法中我试图验证一个非常简单的表单条目。
public function index() {
$this->form_validation->set_rules('status', 'Status', 'trim');
if($this->form_validation->run()) {
echo "true";
$quote = array(
'status' => $this->input->post('status'),
'text' => $this->input->post('text'),
'author' => $this->input->post('author'),
);
$this->quote_model->add($quote);
set_flash_message('Quote has been added.', 'success');
redirect('quote');
}
else {
echo "false";
}
//Some other stuff
由于某种原因,每次我提交表单方法 $this->form_validation->run() 都会返回 FALSE,因此代码最终会回显“false”。但是报价已添加到我的数据库中,并且“报价已添加”。出现 flash 消息,这意味着条件的 TRUE 部分以某种方式被执行。
有人遇到过这个问题吗?我不知道会发生什么。
【问题讨论】:
-
不可能 - 您只使用了
trim并且应该运行它。我认为“虚假”来自其他地方。 - 确保在执行代码之前将$this->form_validation->run()的结果保存到一个变量中并 var_dump 它。但您还应该在运行表单验证之前检查是否有任何发布(添加:&& $this->input->post()到您的条件) -
除了加载视图之外,在我的代码中几乎没有别的东西。我在几个小时前尝试转储结果,但我得到了“布尔错误”。添加条件没有帮助。我只是无法让它以任何方式返回 true。
-
您使用的是什么版本的代码?并且您是否为 form_validation 加载了库?
-
另一种方法是查看完整的 php 源代码并思考什么结果可能返回 false github.com/bcit-ci/CodeIgniter/blob/develop/system/libraries/…
-
我想我明白了。这是因为条件的 TRUE 分支末尾的重定向。耶。
标签: php forms codeigniter validation