表单中的自定义错误处理(Laravel 5)

时间:2015-08-14 02:34:34

标签: laravel laravel-5

对于表单的字段,我可以在我的刀片中使用它,对于表单的字段,它可以很好地处理表单中的错误。

 @if($errors->any())
       <div class="alert alert-error">
       <a href="#" class="close" data-dismiss="alert">&times;</a>
            {!! implode('', $errors->all('<li class="error">:message</li>')) !!}
      </div>
 @endif

public function rules()

但是,我想对表单中的自定义错误使用相同的概念,我相信我无法使用rules()实现。

在我的控制器中,我有:

if ($fileExtension != 'pdf')
   {
        // needs proper handling
   }

如何编辑// needs proper handling部分以便我可以使用相同的刀片概念?

1 个答案:

答案 0 :(得分:1)

您可以使用mime类型规则:

'yourFile' => 'mimes:pdf'

如果您的运行不仅仅是验证,您可以使用这个更长的方法:

$validator = Validator::make(...);

$validator->after(function($validator) {
    if ($fileExtension != 'pdf') {
        #here is place for more stuff...
        $validator->errors()->add('field', 'Something is wrong with this field!');
    }
});