ban*_*cer 7 validation cakephp composite-key
我确信我不是第一个在表中具有复合唯一键并且想要验证它们的人.我不想发明自行车所以我先问这里.我有几个表'id'列作为主键,另外两列作为唯一复合键.最好有一个验证规则来检查提交的条目是否唯一,如果不是则显示验证错误.在Cakephp中,它可以通过自定义验证规则来完成.我很确定有人已经创建了这样的方法.
理想情况下,它将是app_model.php中可以由不同模型使用的方法.
Nik*_*kov 11
我正在使用该功能:
function checkUnique($data, $fields) {
if (!is_array($fields)) {
$fields = array($fields);
}
foreach($fields as $key) {
$tmp[$key] = $this->data[$this->name][$key];
}
if (isset($this->data[$this->name][$this->primaryKey]) && $this->data[$this->name][$this->primaryKey] > 0) {
$tmp[$this->primaryKey." !="] = $this->data[$this->name][$this->primaryKey];
}
//return false;
return $this->isUnique($tmp, false);
}
Run Code Online (Sandbox Code Playgroud)
基本上用法是:
'field1' => array(
'checkUnique' => array(
'rule' => array('checkUnique', array('field1', 'field2')),
'message' => 'This field need to be non-empty and the row need to be unique'
),
),
'field2' => array(
'checkUnique' => array(
'rule' => array('checkUnique', array('field1', 'field2')),
'message' => 'This field need to be non-empty and the row need to be unique'
),
),
Run Code Online (Sandbox Code Playgroud)
所以基本上这将显示每个字段下的警告,说它不是唯一的.
我正在使用它并且它正常工作.
| 归档时间: |
|
| 查看次数: |
2805 次 |
| 最近记录: |