Codeigniter中的验证UNIQUE字段,具有2个索引 [英] Validation UNIQUE field in Codeigniter with 2 index
问题描述
在Codeigniter框架中,我可以使用表单验证类验证MYSQL数据库中的唯一字段。例如:
In the Codeigniter Framework, I can validate an Unique field in the MYSQL Database using the "Form Validation Class". Exemple:
$this->form_validation->set_rules('form_field', 'form_label', 'is_unique[table.field]');
完美地工作,但是,我需要从一个具有2个索引的表中验证一个字段。例如:
Work perfectly, but, I need validate a field from a table with 2 index. Exemple:
UNIQUE INDEX `id_aluno` (`id_aluno`, `ano`),
推荐答案
我不认为 CI
有组合 PK
的内置情况,但我会使用 callback _ 就像这样:但是注意你必须发送第二个 PK
作为额外的规则应该应用在第一个$ PK参见< a href =http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#callbacks =nofollow> callbacks 了解更多信息
I don't think that CI
has build-in case for combined PK
but I would use callback_ like this: but note that you have to send the second PK
as extra and the rule should be applied on the first $PK see callbacks for more info about that
$this->form_validation->set_rules('form_field', 'form_label', 'callback_combpk[$pk2]');
public function combpk($pk1, $pk2)
{
$this->db->where('field1', $pk1);
$this->db->where('field2', $pk2);
$result = $this->db->get('table');
if($result->num_rows() > 0)
{
$this->form_validation->set_message('combpk','something'); // set your message
return false;
}
else{ return true;}
}
这篇关于Codeigniter中的验证UNIQUE字段,具有2个索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!