表单验证在我的 CI 项目中不起作用(已修复,已更新代码)
Form validation is not working in my CI project (fixed, code updated)
我是 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" alt="Logo"></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>
这是我的错误:
Error Number: 1146
Table 'counterpressing.email_id' doesn't exist
SELECT * FROM email_id
WHERE email_id
= 'shakil@gmail.com' LIMIT 1
Filename: D:/Xampp/htdocs/ffbdhub.com/system/database/DB_driver.php
Line Number: 691
我认为你必须加载数据库。像这样
$this->load->database() 在文件中。
这可能会解决您的问题。
不确定是否已提出,但您的验证规则不太正确...您的 "is_unique()" 需要
table and field, so you have the field name which is email_id, but your table name is nowhere to be seen.
因此,无论您的 table 名称是什么,您的 table 包含您要检查的 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 的 table 的名称。
我是 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" alt="Logo"></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>
这是我的错误:
Error Number: 1146
Table 'counterpressing.email_id' doesn't exist
SELECT * FROM
email_id
WHEREemail_id
= 'shakil@gmail.com' LIMIT 1Filename: D:/Xampp/htdocs/ffbdhub.com/system/database/DB_driver.php
Line Number: 691
我认为你必须加载数据库。像这样
$this->load->database() 在文件中。
这可能会解决您的问题。
不确定是否已提出,但您的验证规则不太正确...您的 "is_unique()" 需要
table and field, so you have the field name which is email_id, but your table name is nowhere to be seen.
因此,无论您的 table 名称是什么,您的 table 包含您要检查的 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 的 table 的名称。