代码编辑器中的is_unique用于编辑功能 [英] is_unique in codeigniter for edit function
本文介绍了代码编辑器中的is_unique用于编辑功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有要求,其中我无法对新添加函数中的唯一值进行验证
i have requirement , where i m able to do validation for unique value in new add function like
$this->form_validation->set_rules('email','Email','required|valid_email||is_unique[users.Email]');
它可以工作,但是在编辑功能中不起作用。.我编写了回调函数来检查唯一的电子邮件。
这是我在编辑功能中编写的代码
its working but in edit function its not working..i have written callback function to check unique email. this is code i have written in edit function
$this->form_validation->set_rules('email','Email','required|valid_email|callback_check_email');
function check_username($email)
{
$return_value = $this->user_model->check_email($email);
if ($return_value)
{
$this->form_validation->set_message('email_check', 'Sorry, This username is already used by another user please select another one');
return FALSE;
}
else
{
return TRUE;
}
}
和user_model
and user_model
function check_mail($email)
{
$sql = "SELECT users.Email
FROM users
WHERE
$email = users.Email
";
$result = $this->db->query($sql)->result_array();
return $result;
}
我无法验证唯一电子邮件
i m not able to validate the unique email
推荐答案
在您的控制器中尝试此代码(编辑视图)
Try this code in your controller (edit view)
$original_value = $this->db->query("SELECT EMAIL FROM users WHERE id = ".$id)->row()->EMAIL ;
if($this->input->post('username') != $original_value) {
$is_unique = '|is_unique[users.EMAIL]';
} else {
$is_unique = '';
}
$this->form_validation->set_rules('username', 'User Name', 'required|min_length[3]|max_length[30]|trim|xss_clean'.$is_unique);
这篇关于代码编辑器中的is_unique用于编辑功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文