简体   繁体   English

Laravel-文件的自定义验证不起作用

[英]Laravel - Custom validation for file doesn't work

I am trying to use custom validation rules for file inputs in Laravel. 我正在尝试对Laravel中的文件输入使用自定义验证规则。 The file input name is "photo". 文件输入名称为“照片”。 I am trying to apply a rule called "validate_art", to this file input. 我正在尝试将一个名为“ validate_art”的规则应用于此文件输入。 I know there's "image" validation rule, but I want to apply my own customized rule to this file input. 我知道有“图像”验证规则,但是我想将自己的自定义规则应用于此文件输入。

Here's the code : 这是代码:

  $rules = array('name'=>'required|unique:user,name','startyear'=>'numeric','endyear'=>'numeric'
    ,'photo'=>'validate_art');
    Validator::extend('validate_art',function($attribute,$value,$parameters){
    // returning false just for testing purpose
    return false;
    });

What happens is, it doesn't apply the rule to the field at all. 发生的是,它根本没有将规则应用于字段。 When I try to use this rule to some other field for testing purposes, it works without failure. 当我尝试将此规则用于其他领域进行测试时,它可以正常工作。 Am I doing something wrong? 难道我做错了什么? Or custom validation cannot be applied for file inputs? 还是不能将自定义验证应用于文件输入? Please someone, throw some light at this. 请有人,对此有所说明。 I would really appreciate that. 我真的很感激。

first you should modify your input data 首先,您应该修改输入数据

    foreach ($inputs as $k=>$input){
        if($input instanceOf \Symfony\Component\HttpFoundation\File\UploadedFile)
            $inputs[$k]=$_FILES[$k];
    }

then send $input to validator $validator=Validator::make($input,$rules) 然后将$ input发送到验证器$validator=Validator::make($input,$rules)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM