【问题标题】:Node.js datatables editor - Custom/Unique validation for multiple columnsNode.js 数据表编辑器 - 多列的自定义/唯一验证
【发布时间】:2020-02-03 21:09:19
【问题描述】:

我正在使用数据表编辑器和 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 不是 type=1 且 status=2 时才插入新记录。

这是一个组合多列https://datatables.net/forums/discussion/57729/unique-validation-for-multiple-columns的php示例

->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

【问题讨论】:

    标签: javascript node.js datatables knex.js jquery-datatables-editor


    【解决方案1】:

    有效的是例如...

    .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!';
          }
        }
      }
    })
    

    【讨论】:

      猜你喜欢
      • 2014-12-16
      • 2012-11-09
      • 2019-12-10
      • 2016-01-04
      • 2017-03-02
      • 1970-01-01
      • 2021-09-11
      • 1970-01-01
      • 2016-09-26
      相关资源
      最近更新 更多