【问题标题】:view form validation results using codeigniter使用 codeigniter 查看表单验证结果
【发布时间】:2016-06-01 07:33:28
【问题描述】:

这是表单的 URL。

http://localhost/cig/index.php/Resetpassword?temporarycode=CLK6xW7j0I1psnk243wF&email=coddy@zack.com

这就是我验证它的方式。但问题是我无法在页面上显示验证错误

$this->form_validation->set_rules('password', 'Password', 'required');
            $this->form_validation->set_rules('confirmpassword', 'Password Confirmation', 'required|matches[password]');

            if ($this->form_validation->run() == FALSE) {

              redirect($_SERVER['HTTP_REFERER']);
          }

【问题讨论】:

  • 请同时发布查看代码。

标签: php forms codeigniter validation url


【解决方案1】:

好的,我遇到了一些可以改进的地方。


对于初学者来说,当字段无效时,为什么要将用户重定向到某个地方?为什么不使用:

$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('confirmpassword', 'Password Confirmation', 'required|matches[password]');
if ($this->form_validation->run()) {
    redirect(base_url('emailSend'); // or wherever you want the visitor to be send to
} 
$this->load->view('register'); // or however your view file is called

这样,如果您的视图文件中有规则<?= validation_errors() ?>,则显示错误没有问题,<?= ?><?php echo ?> 的简短 php 代码,validation_erros() 应该包含验证错误(如果有)任何。

【讨论】:

  • 哦!实际上我没有尝试过,认为它不会引起问题并且不起作用。但它奏效了。谢谢
  • np 很高兴为您提供帮助
【解决方案2】:

你可以试试CI flash data,

$this->session->set_flashdata('errors', validation_errors());

redirect($_SERVER['HTTP_REFERER']);

它保留下一个服务器请求的数据。

【讨论】:

  • 感谢这个想法。但是你能告诉我如何看到并展示它吗?
  • 在视图中,使用$this->session->flashdata('errors');显示消息
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-02-09
  • 2017-02-06
  • 2016-09-05
  • 2021-10-09
  • 1970-01-01
相关资源
最近更新 更多