haa*_*kym 8 php validation laravel laravel-5
我有一个表单,一个字段用于名为 的文件,在我的请求中,当使用验证规则将attachment另一个名为 的字段requestType设置为 的值时,该字段的验证要求该字段存在。sickrequired_if
我面临的问题是,即使我为相关输入字段上传文件,请求类中的验证规则仍然会被触发:The attachment field is required when request type is sick.
这是我的代码:
\n\nrequired请注意,字段上的html属性attachment并不是导致问题的原因,在页面加载时将其设置为“ sick” disabled,当requestType设置为“sick”时,该disabled属性将被删除。
看法
\n\n{!! Form::open([\'route\' => \'employee.request.store\', \'class\' => \'form-horizontal\', \'id\' => \'\', \'files\' => \'true\']) !!}\n\n <div class="form-group {{ $errors->first(\'requestType\', \'has-error\') }}">\n <label for="" class="col-sm-2 control-label"> {{ Lang::get(\'employee_request_contractor_create.request_type\') }} *</label>\n <div class="col-sm-3">\n {!! \n Form::select(\'requestType\', \n [\'\' => \'Select\', \'normal\' => \'Normal\', \'emergency\' => \'Emergency\', \'sick\' => \'Sick\'], \n \'\', \n [\'class\' => \'form-control\', \'id\' => \'requestType\', \'required\' => \'required\']\n ) \n !!}\n </div>\n {!! $errors->first(\'requestType\', \'<label class="col-sm-3 control-label" for="">:message</label>\') !!}\n </div>\n\n <div class="form-group {{ $errors->first(\'attachment\', \'has-error\') }}" id="attachmentFormGroup">\n <label for="" class="col-sm-2 control-label"> {{ Lang::get(\'employee_request_contractor_create.attachment\') }} <small>(Sick only)</small> </label>\n <div class="col-sm-3">\n <input type="file" name="attachment" id="attachment" required="required">\n <label>(Please provide HR with original copy)</label>\n </div>\n {!! $errors->first(\'attachment\', \'<label class="col-sm-3 control-label" for="">:message</label>\') !!}\n </div>\n <!-- other form inputs and submit button -->\n{!! Form::close() !!}\nRun Code Online (Sandbox Code Playgroud)\n\n要求
\n\npublic function rules()\n{\n return [\n \'requestType\' => \'required|max:255\',\n \'attachment\' => \'required_if:requestType,sick|mimes:pdf,jpg,png,gif,jpeg|max:512\',\n /* other rules */\n ];\n\n}\nRun Code Online (Sandbox Code Playgroud)\n\n如果我删除required_if:requestType附件上传就好了并且如果我在控制器中输出:
if(\\Input::hasFile(\'attachment\') echo \'true\';\nRun Code Online (Sandbox Code Playgroud)\n\n我会看到真实的。
\n\n当我在控制器存储方法中 dd($request) 时,我看到以下内容(相关部分):
\n\n+request: ParameterBag {#227 \xe2\x96\xbc\n #parameters: array:10 [\xe2\x96\xbc\n "_token" => "XkQwP608M5WQ4qtHCYN0dIVETDeqzL0E5ZI99iSf"\n "requestType" => "sick"\n "manager" => "2"\n "dateFrom" => "01-06-2015"\n "dateFromHijri" => "1436-08-14"\n "dateTo" => "02-06-2015"\n "dateToHijri" => "1436-08-15"\n "noOfDays" => "2"\n "location" => "London"\n "contactNumber" => "123456"\n ]\n}\nRun Code Online (Sandbox Code Playgroud)\n\n和...
\n\n+files: FileBag {#221 \xe2\x96\xbc\n #parameters: array:1 [\xe2\x96\xbc\n "attachment" => UploadedFile {#27 \xe2\x96\xbc\n -test: false\n -originalName: "test_doc.pdf"\n -mimeType: "application/pdf"\n -size: 82584\n -error: 0\n }\n ]\n }\nRun Code Online (Sandbox Code Playgroud)\n\n规则是否因为附件未与其他请求属性一起显示而被解雇?
\n\n更新:错误消息:
\n\n["errors"]=>\n object(Illuminate\\Support\\ViewErrorBag)#178 (1) {\n ["bags":protected]=>\n array(1) {\n ["default"]=>\n object(Illuminate\\Support\\MessageBag)#179 (2) {\n ["messages":protected]=>\n array(1) {\n ["attachment"]=>\n array(1) {\n [0]=>\n string(59) "The attachment field is required when request type is sick."\n }\n }\n ["format":protected]=>\n string(8) ":message"\n }\n }\n }\nRun Code Online (Sandbox Code Playgroud)\n\n任何帮助将非常感激。谢谢!
\nrequestType由于未设置为 时输入被禁用sick,因此附件不会包含在POST发送到服务器的请求中。这就是 Laravel 的请求验证文件触发的原因,因为它甚至看不到数据中的值POST。要解决这个问题,只需添加sometimes该字段的验证字符串即可attachment。代码应该如下所示:
public function rules()
{
return [
'requestType' => 'required|max:255',
'attachment' => 'sometimes|required_if:requestType,sick|mimes:pdf,jpg,png,gif,jpeg|max:512',
/* other rules */
];
}
Run Code Online (Sandbox Code Playgroud)
以下是Laravel 文档中有关此问题的摘录:
在某些情况下,您可能希望仅当正在验证的数据中存在该字段时才对该字段运行验证检查。要快速完成此操作,请将“有时”规则添加到规则列表中:
Run Code Online (Sandbox Code Playgroud)$v = Validator::make($data, [ 'email' => 'sometimes|required|email', ]);在上面的示例中,仅当电子邮件字段出现在 $data 数组中时才会被验证。
| 归档时间: |
|
| 查看次数: |
4667 次 |
| 最近记录: |