我的验证表单运行良好,看起来像这样:
{!! Form::email('email', null, array('placeholder' => 'Email', 'class' => 'form-control ')) !!}
{{$errors->first('email')}}
如果电子邮件不好,我会收到以下错误:电子邮件已经被接收
问题是,我不想像error
一样在输入上放一个css类来为输入添加红色背景边框。
我该怎么做?
您可以使用if条件检查错误:
<div class="form-group {{ $errors->has('email') ? 'has-error' :'' }}">
{!! Form::text('email',null,['class'=>'form-control','placeholder'=>'Email Address']) !!}
{!! $errors->first('email','<span class="help-block">:message</span>') !!}
</div>
这是我的正确答案,没有使用额外的div's
:
{!! Form::email('email', null, $attributes = $errors->has('email') ? array('placeholder' => 'Email', 'class' => 'form-control has-error') : array('placeholder' => 'Email', 'class' => 'form-control')) !!}
{{$errors->first('email')}}
您可以使用以下代码检查电子邮件字段是否存在任何验证错误
<div class="form-group has-feedback @if($errors->has('email') has-error @endif">
<input name="email" class="form-control"/>
<span class="glyphicon glyphicon-user form-control-feedback"></span>
@if ($errors->has('email')) <p class="help-block"><span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>
{{ $errors->first('email') }}</p>
@endif
</div>
您可以在它周围添加HTML,并根据需要进行样式设置。
HTML:
<span class="error">{{$errors->first('email')}}</span>
CSS:
.error {
border: 1px solid red;
}
编辑:将has-error
类添加到输入本身,如下所示:
{!! Form::email('email', null, array('placeholder' => 'Email', 'class' => 'form-control ' . $errors->first('email', 'has-error'))) !!}
添加自定义错误消息
'custom' => array(
'email' => array(
'required' => 'We need to know your e-mail address!',
),
),
Laravel 中的自定义错误消息
<script>
if({{$errors->has('email')}}){
$(input[type="email"]).css('border-color', 'red');
}
</script>
add class在表单中出现错误,如
{!! Form::email('email', null, array('placeholder' => 'Email', 'class' => 'form-control has-error')) !!}
如果出现错误,可以使用if条件,在中设置类
@if($errors->has('email'))
{!! Form::email('email', null, array('placeholder' => 'Email', 'class' => 'form-control has-error')) !!}
@else
{!! Form::email('email', null, array('placeholder' => 'Email', 'class' => 'form-control')) !!}
@endif
或者你可以像这个一样
<input class="form-control {{ $errors->has('email') ? 'has-error' : '' }}" name="email" placeholder="Email">