不要这样。
我必须验证基本表格。我使用CI form_validation库,表单验证规则只设置为&#34; required&#34; ...如果表单验证失败,它只需要在登录表单中回显验证错误。我默认使用<?php echo validation_errors(); ?>
Helper是自动加载的。表单功能是好的它应该如何工作,唯一的问题是框架不显示错误消息如果不填写必填字段?
login_form.php - 查看
<?php echo validation_errors(); ?>
<?php echo form_input('email' , 'email'); ?> <br>
<?php echo form_input('password' , 'password'); ?> <br>
<?php echo form_submit('submit' , 'submit'); ?>
login.php - CONTROLLER
<?php
Class Login extends CI_Controller {
function index () {
$data ['content'] = "login_form";
$this->load->view("template", $data);
}
function validate() {
$this->load->library("form_validation");
$this->form_validation->set_rules('email' , 'email' , 'trim|xss_clean|required');
$this->form_validation->set_rules('password' , 'password' , 'md5|trim|required');
if( $this->form_validation->run() == FALSE) {
redirect('login/index');
}
else {
redirect("user/profile");
}
}
}
?>
答案 0 :(得分:1)
为什么不在控制器中使用代码,希望能帮助您将表单操作设置为login.php login.php - CONTROLLER
function index () {
$data ['content'] = "login_form";
$this->load->library("form_validation");
$this->form_validation->set_rules('email' , 'email' , 'trim|xss_clean|required');
$this->form_validation->set_rules('password' , 'password' , 'md5|trim|required');
if( $this->form_validation->run() == FALSE) {
$this->load->view("template", $data);
}
else {
redirect("user/profile");
}
}
login_form.php - VIEW
<?php echo validation_errors(); ?>
<?php echo form_input('email' , 'email'); ?> <br>
<?php echo form_input('password' , 'password'); ?> <br>
<?php echo form_submit('submit' , 'submit'); ?>
答案 1 :(得分:0)
if( $this->form_validation->run() == FALSE) {
redirect('login/index');
}
不要重定向,否则错误将无法使用。而不是保持
if( $this->form_validation->run() == FALSE) {
$data ['content'] = "login_form";
$this->load->view("template", $data);
}
检查以下链接以获取更多详细信息:
Form Validation : CodeIgniter User Guide