使用编辑器数据表中的其他字段值的验证字段

时间:2019-07-08 22:38:34

标签: php datatables

我的数据库如下:

Editor::inst( $db, 'servidores' )
->fields(
    Field::inst('id'),
    Field::inst('user_id')
    ->validator(Validate::required(ValidateOptions::inst()
        ->message('Required field!')
    ))
    ->validator(function($val, $data){
        $result = mysqli_query($con, "select * from table where data= ????? and user_id= '" . $val . "'");
    if (mysqli_num_rows($result) > 0){
        return false;
    }else{
        return true;
    }),
    Field::inst('date')
    ->validator(Validate::required(ValidateOptions::inst()
        ->message('Required field!')
    ))
)
->process( $_POST )
->json();

如果数据库中存在user_iddate,我需要阻止插入,例如:

user_id | data
      1 |    1
      2 |    1
      2 |    2
      2 |    1 //wrong

我尝试像这样验证:

->validator(function($val, $data){
    $result = mysqli_query($con, "select * from table where data= ????? and user_id= '" . $val . "'");
if (mysqli_num_rows($result) > 0){
    return false;
}else{
    return true;
}),

但是我无法获得data值以用于我的选择。

2 个答案:

答案 0 :(得分:2)

尝试以下修改后的查询

  $result = mysqli_query($con, "select * from table where data= '&data' and user_id= '" . $val . "'");

答案 1 :(得分:1)

解决方法是:

->validator( function ($editor, $action, $data) {
    if ( $action == Editor::ACTION_EDIT || $action == Editor::ACTION_CREATE) {
        foreach ($data['data'] as $key => $values) {
            $pkey = $editor->pkeyToArray( $key );
                $any = $editor->db()->any( 'servidores', function ($q) use ($pkey, $values) {
                    $q->where( 'cidadao_id', $values['servidores']['user_id']);
                    $q->where( 'data', $values['servidores']['data'] );
                });

                if ( $any ) {
                    return 'False';
                }
            }
        }
    }
)