表单验证在我的 CI 项目中不起作用(已修复,代码已更新)
Posted
技术标签:
【中文标题】表单验证在我的 CI 项目中不起作用(已修复,代码已更新)【英文标题】:Form validation is not working in my CI project (fixed, code updated) 【发布时间】:2017-06-25 05:23:47 【问题描述】:我是 CodeIgniter 的新手。我正在尝试在用户注册中实现表单验证。
$autoload['libraries'] = array('database','session','encrypt','form_validation');
$autoload['helper'] = array('url','file','form');
$autoload['model'] = array('user_model');
控制器代码:
class User_registration extends CI_Controller
public function index()
$this->register();
function register()
//set validation rules
$this->load->library('form_validation');
$this->form_validation->set_rules('user_name', 'First Name', 'trim|required|alpha|min_length[3]|max_length[30]|xss_clean');
$this->form_validation->set_rules('email_id', 'Email ID', 'trim|required|valid_email|is_unique[email_id]');
$this->form_validation->set_rules('user_password', 'Password', 'trim|required|matches[confirm_password]|md5');
$this->form_validation->set_rules('confirm_password', 'Confirm Password', 'trim|required|md5');
//validate form input
if ($this->form_validation->run() == FALSE)
// fails
$data = array();
$data['title'] = 'Registration | Admin Panel';
$data['header_content'] = $this->load->view('adminEntry/header_content', '', true);
$data['footer_content'] = $this->load->view('adminEntry/footer_content', '', true);
$this->load->view('adminentry/user_registration', $data);
else
//insert the user registration details into database
echo '<pre>';
print_r($data);
exit();
$data = array(
'user_name' => $this->input->post('user_name'),
'email_id' => $this->input->post('email_id'),
'password' => $this->input->post('user_password'),
);
if ($this->user_model->insertuser($data))
if ($this->user_model->sendEmail($this->input->post('email_id')))
// successfully sent mail
$this->session->set_flashdata('msg', '<div class="alert alert-success text-center">You are Successfully Registered! Please confirm the mail sent to your Email-ID!!!</div>');
redirect('user_registration');
else
// error
$this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Oops! Error. Please try again later!!!</div>');
redirect('user_registration');
else
// error
$this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Oops! Error. Please try again later!!!</div>');
redirect('user_registration');
用户模型:
class User_model extends CI_Model
//insert into user table
function insertUser($data)
return $this->db->insert('user', $data);
function sendEmail($to_email)
$from_email = 'sample@mail.com'; //change this to yours
$subject = 'Verify Your Email Address';
$message = 'Dear User,<br /><br />Please click on the below activation link to verify your email address.<br /><br /> http://example.com/user/verify/' . md5($to_email) . '<br /><br /><br />Thanks<br />Mydomain Team';
//configure email settings
$config['protocol'] = 'smtp';
$config['smtp_host'] = 'ssl://smtp.example.com'; //smtp host name
$config['smtp_port'] = '465'; //smtp port number
$config['smtp_user'] = $from_email;
$config['smtp_pass'] = 'examplepass'; //$from_email password
$config['mailtype'] = 'html';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;
$config['newline'] = "\r\n"; //use double quotes
$this->email->initialize($config);
//send mail
$this->email->from($from_email, 'example.com');
$this->email->to($to_email);
$this->email->subject($subject);
$this->email->message($message);
return $this->email->send();
//activate user account
function verifyEmailID($key)
$data = array('approval_status' => 1);
$this->db->where('md5(email_id)', $key);
return $this->db->update('user', $data);
查看页面:
<div class="container">
<div class="full-content-center animated fadeInDownBig">
<p class="text-center"><a href="#"><img src="<?php echo base_url(); ?>adminAssets/img/login-logo.png" ></a></p>
<div class="login-wrap">
<div class="row">
<div class="col-sm-6">
<?php echo $this->session->flashdata('verify_msg'); ?>
</div>
</div>
</div>
<div class="login-block">
<?php
$attributes = array("name" => "registrationform");
echo form_open("user_registration/index", $attributes);
?>
<form role="form" method="post" action="<?php echo base_url(); ?>user_registration" enctype="multipart/form-data>">
<div class="form-group login-input">
<i class="fa fa-user overlay"></i>
<input type="text" name="user_name" value="<?php echo set_value('user_name'); ?>" class="form-control text-input" placeholder="Name">
<span class="text-danger"><?php echo form_error('user_name'); ?></span>
</div>
<div class="form-group login-input">
<i class="fa fa-envelope overlay"></i>
<input type="text" name="email_id" value="<?php echo set_value('email_id'); ?>" class="form-control text-input" placeholder="E-mail">
<span class="text-danger"><?php echo form_error('email_id'); ?></span>
</div>
<div class="form-group login-input">
<i class="fa fa-key overlay"></i>
<input type="password" name="user_password" value="<?php echo set_value('user_password'); ?>" class="form-control text-input" placeholder="Password" id="txtNewPassword">
<span class="text-danger"><?php echo form_error('user_password'); ?></span>
</div>
<div class="form-group login-input">
<i class="fa fa-key overlay"></i>
<input type="password" name="confirm_password" value="<?php echo set_value('confirm_password'); ?>" class="form-control text-input" placeholder="Confirm Password" id="txtConfirmPassword" onChange="isPasswordMatch();" >
</div>
<div class="form-group login-input" id="divCheckPassword"></div>
<div class="row">
<div class="col-sm-12">
<button type="submit" name="submit" class="btn btn-default btn-block">Register</button>
</div>
</div>
<?php echo form_close(); ?>
<?php echo $this->session->flashdata('msg'); ?>
</form>
</div>
</div>
</div>
</div>
这是我的错误:
错误号:1146
表“counterpressing.email_id”不存在
SELECT * FROM
email_id
WHEREemail_id
= 'shakil@gmail.com' 限制 1文件名:D:/Xampp/htdocs/ffbdhub.com/system/database/DB_driver.php
行号:691
【问题讨论】:
查看要注册的表单打开更改索引 我试过了,没用! :(我附上了错误的屏幕截图。如果你有时间请检查:) 【参考方案1】:我认为你必须加载数据库。像这样
$this->load->database() 在文件中。
这可能会解决您的问题。
【讨论】:
没用!我尽我所能。我尝试了简单的注册(只是将数据插入数据库)。有效。但是当我使用表单验证和电子邮件激活功能时。它不工作:/【参考方案2】:不确定这是否已经提出,但您的验证规则不太正确...您的“is_unique()”需要
表和字段,所以你的字段名是email_id,但是你的表名却看不到。
因此,无论您要检查的包含 email_id 的表的表名称是什么,您都需要更改它...
$this->form_validation->set_rules('email_id', 'Email ID', 'trim|required|valid_email|is_unique[email_id]');
到这里
$this->form_validation->set_rules('email_id', 'Email ID', 'trim|required|valid_email|is_unique[table_name.email_id]');
简单来说,您需要有 is_unique[table_name.email_id]
,其中 table_name 是包含您正在测试的 email_id 的表的名称。
【讨论】:
是的,我昨晚终于解决了 :) 感谢您的评论!我错过了那部分,我想不通!愚蠢的错误!以上是关于表单验证在我的 CI 项目中不起作用(已修复,代码已更新)的主要内容,如果未能解决你的问题,请参考以下文章
为啥 jquery.validation.js 在我的项目中不起作用?