如何在laravel 5的刀片模板上显示错误消息

时间:2016-08-09 04:30:43

标签: php laravel-5

我是laravel 5.2创建的项目。我一直在创建登录页面,但我坚持在刀片模板上显示验证错误消息。我不知道我做错了什么?

请帮我解决这个问题。谢谢!

查看

# Add to your settings file
CONTENT_TYPES = ['image', 'video']
# 2.5MB - 2621440
# 5MB - 5242880
# 10MB - 10485760
# 20MB - 20971520
# 50MB - 5242880
# 100MB 104857600
# 250MB - 214958080
# 500MB - 429916160
MAX_UPLOAD_SIZE = 5242880

#Add to a form containing a FileField and change the field names accordingly.
from django.template.defaultfilters import filesizeformat
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
def clean_content(self):
    content = self.cleaned_data['content']
    content_type = content.content_type.split('/')[0]
    if content_type in settings.CONTENT_TYPES:
        if content._size > settings.MAX_UPLOAD_SIZE:
            raise forms.ValidationError(_('Please keep filesize under %s. Current filesize %s') % (filesizeformat(settings.MAX_UPLOAD_SIZE), filesizeformat(content._size)))
    else:
        raise forms.ValidationError(_('File type is not supported'))
    return content

控制器

 <div>
    <input type="text" name="email" class="form-control" placeholder="Email"/>
    @if($errors->has('email')) <p>{{ $errors->first('email') }}</p> @endif
 </div>

2 个答案:

答案 0 :(得分:1)

您无需检查错误,只需执行以下操作

{!! $errors->first('email', '<dev class="error-block">:message</div>') !!}

请注意“error-block”是一个自定义css类,您可以根据自己的选择自定义html模板。

Retrieving An Error Message With A Format

答案 1 :(得分:0)

我有不同的方法。

在输入字段下方,我包含了额外的视图:

@include('input-errors', ['inputName' => 'inputName']) #For your case it would be 'email'

input-errors.blade.php

@foreach ($errors->get($inputName) as $message)
    <span class="input-error">{{ $message }}</span>
@endforeach

样式.css

.input-error {
    color: #ff5555;
}