Laravel自定义验证器功能未被调用

时间:2017-02-05 02:22:43

标签: php validation laravel-5.4

我正在尝试为验证器添加自定义规则,但它根本不起作用,甚至没有调用该函数。我从文档(https://laravel.com/docs/5.4/validation#custom-validation-rules)中获取了这个:

在AppServiceProvider :: boot中我有这个:

Validator::extend('foo', function ($attribute, $value, $parameters, $validator) {
   return false;
});

并且在我的控制器中我有这个:

$validator = Validator::make($request->all(), [
    'myField' => 'foo',
]);

验证器不会失败。我做错了什么?

1 个答案:

答案 0 :(得分:1)

修正了它。我的json错了,如果输入值为null(或为空),则不会检查自定义Validator :: extend规则。所以解决方案就是

def main():
  data = []
  for line in open('Magic Square Input.txt'):
    items = line.split(" ")
    temp = 0
    setOfThree = []
    for item in items:

        if(len(item) > 1):
            item = item[0:1]

        if(item != " "):
            setOfThree.append(item)
            temp += 1


        if(temp == 3):
            test = []
            test = setOfThree[:]
            test = [int(x) for x in test]
            data.append(test)

或$ request非空的字段