Node.js 数据表编辑器 - Custom/Unique 多列验证
Node.js datatables editor - Custom/Unique validation for multiple columns
我正在使用数据表编辑器和 node.js。 Validator.dbUnique() 让我在插入之前验证该值是否在该列中是唯一的。参考https://editor.datatables.net/manual/nodejs/validation
new Field( 'groups.name' )
new Field( 'groups.product_id' ),
.validator( Validator.dbUnique() )
new Field( 'groups.type' )
new Field( 'groups.status' );
不过,我需要的是对多列进行验证。因此,仅当 product_id IS NOT of type=1 and status=2 found 时才插入新记录。
这是一个 php 组合多列的示例 https://datatables.net/forums/discussion/57729/unique-validation-for-multiple-columns
->validator( function($editor, $action, $data){
if ($action === Editor::ACTION_CREATE || $action === Editor::ACTION_EDIT){
foreach ($data['data'] as $pkey => $values ){
$count = $editor->db()->query('select')->get('*')->table('teachers_subjects')
->where('teachers_subjects.teacher', $values['teachers_subjects']['teacher'])
->where('teachers_subjects.subject', $values['teachers_subjects']['subject'])
->exec()
->count();
if ($count == 1){
return 'This selection is already in the database.';
}
}
}
})
但是如何使用 node.js 呢?
射频。 https://editor.datatables.net/manual/nodejs/validation#Global-validators
有效的是例如...
.validator( async (editor, action, data) => {
if (action === 'create') {
for (let [key, value] of Object.entries(data.data)) {
let check = await editor
.db()
.from('groups')
.where('groups.product_id', value.groups.product_id)
.where('groups.type', value.groups.type)
.where('groups.status', '=', '2');
if (check.length > 0) {
return 'Record already exists!';
}
}
}
})
我正在使用数据表编辑器和 node.js。 Validator.dbUnique() 让我在插入之前验证该值是否在该列中是唯一的。参考https://editor.datatables.net/manual/nodejs/validation
new Field( 'groups.name' )
new Field( 'groups.product_id' ),
.validator( Validator.dbUnique() )
new Field( 'groups.type' )
new Field( 'groups.status' );
不过,我需要的是对多列进行验证。因此,仅当 product_id IS NOT of type=1 and status=2 found 时才插入新记录。
这是一个 php 组合多列的示例 https://datatables.net/forums/discussion/57729/unique-validation-for-multiple-columns
->validator( function($editor, $action, $data){
if ($action === Editor::ACTION_CREATE || $action === Editor::ACTION_EDIT){
foreach ($data['data'] as $pkey => $values ){
$count = $editor->db()->query('select')->get('*')->table('teachers_subjects')
->where('teachers_subjects.teacher', $values['teachers_subjects']['teacher'])
->where('teachers_subjects.subject', $values['teachers_subjects']['subject'])
->exec()
->count();
if ($count == 1){
return 'This selection is already in the database.';
}
}
}
})
但是如何使用 node.js 呢?
射频。 https://editor.datatables.net/manual/nodejs/validation#Global-validators
有效的是例如...
.validator( async (editor, action, data) => {
if (action === 'create') {
for (let [key, value] of Object.entries(data.data)) {
let check = await editor
.db()
.from('groups')
.where('groups.product_id', value.groups.product_id)
.where('groups.type', value.groups.type)
.where('groups.status', '=', '2');
if (check.length > 0) {
return 'Record already exists!';
}
}
}
})