laravel数组密钥验证规则

时间:2017-02-11 08:55:15

标签: php arrays laravel validation laravel-5.3

我想验证一个它所要求的数组:

  "accountType": {
    "admin" : true,
    "advertiser": true,
    "publisher": true,
    "agency": true
  },

我想检查admin是否为true,什么都不做并传递,但如果admin为false且其他为true或者accountType对象验证中没有admin会抛出错误,如:无效的帐户类型。

在另一个词中,我想检查请求数组中是否有管理员通过验证,如果没有,并且有其他类型显示错误,反之亦然。

这是我的验证,但它无论如何都会通过:

 $validator = Validator::make($this->request->all(), [
            'accountType.admin' => 'boolean:true',
            'accountType.advertiser' => 'boolean:false',
            'accountType.publisher' => 'boolean:false',
            'accountType.agency' => 'boolean:false',
        ]);

3 个答案:

答案 0 :(得分:1)

尝试

$validator = Validator::make($this->request->all(), [
            'accountType.admin' => 'required|boolean:true',
            'accountType.advertiser' => 'boolean:false',
            'accountType.publisher' => 'boolean:false',
            'accountType.agency' => 'boolean:false',
        ]);

答案 1 :(得分:0)

应该从文档中做到这一点:验证中的字段必须能够被转换为布尔值。接受的输入为true,false,1,0,“1”和“0”。

$validator = Validator::make($this->request->all(), [
    'accountType.admin' => 'boolean',
    'accountType.advertiser' => 'boolean',
    'accountType.publisher' => 'boolean',
    'accountType.agency' => 'boolean',
]);

答案 2 :(得分:0)

您可以将值更改为1表示“真​​”,将0更改为“假”,然后验证为:

$validator = Validator::make($this->request->all(), [
    'accountType.admin' => 'required|min:1',
    'accountType.advertiser' => 'required|min:1',
    'accountType.publisher' => 'required|min:1',
    'accountType.agency' => 'required|min:1',
]);
相关问题