表单验证不起作用CodeIgniter 3


public function blog_create()
{
$this->load->library('form_validation');
$this->form_validation->set_rules("email", 'Email', "trim|required|valid_email|max_length[100]");
$this->form_validation->set_rules("password", "Password", "trim|required|min_length[8]");
$blog_name = $this->input->post('name');
$blog_des = $this->input->post('description');
$user_id = $this->session->userdata('user_id');
if ($this->form_validation->run() == TRUE){
$pass = $this->Blog_Model->blog_insert($blog_name,$blog_des,$user_id);
if ($pass === TRUE) {
$this->session->set_flashdata('blogin','Blog Was Inserted');
return redirect('dashboard?success="success"');
}
}
redirect('dashboard?rr="error"');
}

表单验证不起作用我不知道出了什么问题我在索引文件中放入了validation_error,但它仍然存在没有显示任何错误

所以,我用你的代码创建了一个演示,它对我很有效。我看到的问题应该是你没有在view中打印successerror消息。我已经写了下面的代码,并在必要时在评论中进行了解释。看看它是否对你有帮助

查看(博客表单(

<?php $this->load->view('your-path/vwError'); // load a view to show errors ?> 
<form method="POST" action="<?php echo base_url('form'); ?>">
<input type="text" name="email">
<input type="password" name="password">
<input type="submit" value="Submit" > 
</form>

视图(错误(

if($this->session->flashdata('message')){
$message = $this->session->flashdata('message');
}
if($this->session->flashdata('error')){
$error = $this->session->flashdata('error');
}
<?php if (!empty($message)): ?>
<div class="alert alert-success"> <!-- your-style-here, I've used bootstrap here(Green background) -->
<a class="close" data-dismiss="alert">×</a>
<?php echo $message; ?>
</div>
<?php endif; ?>
<?php if (!empty($error)): ?>
<div class="alert alert-danger"> <!-- your-style-here, I've used bootstrap here(Red background) -->
<a class="close" data-dismiss="alert">×</a>
<?php echo $error; ?>
</div>
<?php endif; ?>

控制器

function form(){
$this->form_validation->set_rules("email", 'Email', "trim|required|valid_email|max_length[100]");
$this->form_validation->set_rules("password", "Password", "trim|required|min_length[8]");
if($this->form_validation->run() == TRUE){  // validation successful
// get the value if the validation is successful otherwise it's just extra code
$blog_name = $this->input->post('name');
$blog_des = $this->input->post('description');
$user_id = $this->session->userdata('user_id');
$pass = $this->Blog_Model->blog_insert($blog_name,$blog_des,$user_id); // your-logic
if ($pass === TRUE) {
$this->session->set_flashdata('message', 'Blog Was Inserted');
redirect('your-url-here');
} // you can also set a flashdata here in {else} condition if the data isn't saved ie returns FALSE
}else{  // validation failed
$this->session->set_flashdata('error', validation_errors()); // store the validation errors in flashdata
$this->load->view('your-blog-form-view');   // You probably don't want to redirect as you'd want to retain the previous input value from the user.
}
}

首先调试代码,并告诉我验证是否正确运行

$user_id = $this->session->userdata('user_id');
if ($this->form_validation->run() == FALSE) {
echo validation_errors();     
}

而不是检查:

If($this->form_validation->run() == true)
{
// Do what needs to be done when validation passes
}

尝试检查相反的情况:

if($this->form_validation->run() == false)
{
// Do something if validation fails
}
else
{
// Do what needs to be done when validation passes
}

在大多数松散类型的语言中,不是false的东西不一定是true。如果验证失败,表单验证将保证响应false,但您不能仅基于这一事实就认为成功的验证将响应true

相关内容

  • 没有找到相关文章