如何在codeigniter中使用FormValidation
是否有可能如何显示控制器中的字符串以在 codeigniter 中查看? 我正在进行表单验证以检查数据库中是否存在特定记录。
这是我的控制器的代码
function create_customer()
{
// field name, error message, validation rules
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|callback_username_exists');
$this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');
if($this->form_validation->run() == FALSE)
{
$this->load->view('admin/users/admin_form');
}
else
{
if($query = $this->usermodel->create_customer())
{
redirect('userslist');
}
else
{
$this->load->view('admin/users/admin_form.php');
}
}
}
function username_exists($key)
{
$this->usermodel->username_exists($this->input->post('username'));
}
这是我的模型的代码
function create_customer()
{
$new_member_insert_data = array(
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'password ' => md5($this->input->post('password '))
);
$insert = $this->db->insert('users', $new_member_insert_data);
return $insert;
}
function username_exists($key)
{
$this->db->where('username',$this->input->post('username'));
$query = $this->db->get('users');
if ($query->num_rows() > 0){
return true;
}
else{
return false;
}
}
我想要发生的是;我将如何显示类似“用户名已存在”的预期错误消息,并且我想在我的视图中显示它。这可能吗?或者我应该尝试采取另一种方法?
Is it possible how to display string from controller to view in codeigniter?
im doing a form validation to check if a certain record exists in the database.
here's the code for my controller
function create_customer()
{
// field name, error message, validation rules
$this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[4]|callback_username_exists');
$this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this->form_validation->set_rules('password2', 'Password Confirmation', 'trim|required|matches[password]');
if($this->form_validation->run() == FALSE)
{
$this->load->view('admin/users/admin_form');
}
else
{
if($query = $this->usermodel->create_customer())
{
redirect('userslist');
}
else
{
$this->load->view('admin/users/admin_form.php');
}
}
}
function username_exists($key)
{
$this->usermodel->username_exists($this->input->post('username'));
}
here's the code for my model
function create_customer()
{
$new_member_insert_data = array(
'username' => $this->input->post('username'),
'email' => $this->input->post('email'),
'password ' => md5($this->input->post('password '))
);
$insert = $this->db->insert('users', $new_member_insert_data);
return $insert;
}
function username_exists($key)
{
$this->db->where('username',$this->input->post('username'));
$query = $this->db->get('users');
if ($query->num_rows() > 0){
return true;
}
else{
return false;
}
}
what i wanted to happen is; how will i show an expected error message something like this "Username already exists" and i want to display it in my view. is this possible? or should i try to do another approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以在此处进行学习。但为了让您更好地理解,我将解释整个事情。您的控制器应如下所示 这
是您的模型的代码
您的视图应包含以下行:
如果您仍然无法理解,请让我知道。
You can study here. But for your better understanding, I will explain the whole thing. Your Controller should look like this
Here's the Code for your Model
Your View should contain the following line:
If you still have problem to understand please let me know.
使用
您可以在控制器上 :
$this->session->set_flashdata('message', '
');
然后只需调用您的视图即可:
session->flashdata('message'); ?>
希望有帮助
You can use
On controller:
$this->session->set_flashdata('message', '<div class="message">YES!</div>');
Then just call on your view:
<?php echo $this->session->flashdata('message'); ?>
Hope that helps
由于您使用的是表单验证类,因此视图中存在错误消息。您只需使用
form_error()
调用错误消息即可。有关更多详细信息,请访问http://codeigniter.com/user_guide/libraries/form_validation.html。同样要传递变量给view,调用view显示的时候需要传递
$this->load->view('YOUR_VIEW', array('VARIABLE_NAME' => "VARIABLE_VALUE"));
Since you are using Form Validation class, in view the error messages are present. All you need to call the error message using
form_error()
. For more details please visit http://codeigniter.com/user_guide/libraries/form_validation.html.Also to pass a variable to view, you need to pass it when you call a view to display
$this->load->view('YOUR_VIEW', array('VARIABLE_NAME' => "VARIABLE_VALUE"));
您可以在控制器类中定义变量。
home_main.php:
这是一个有用的方法。
You can define a variable in controller class.
home_main.php:
It is an usefull method.