Laravel 5 验证 - 表单元素上的错误类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33538606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:38:50  来源:igfitidea点击:

Laravel 5 Validation - error class on form elements

phpcsslaravellaravel-4laravel-5

提问by paulalexandru

My validation form works fine and it looks like this:

我的验证表单工作正常,它看起来像这样:

{!! Form::email('email', null, array('placeholder' => 'Email', 'class' => 'form-control ')) !!}
{{$errors->first('email')}}

If the email is not good, I get this error: The a email has already been taken.

如果电子邮件不好,我会收到此错误消息:电子邮件已被占用。

The thing is that I wan't to put a css class on the input also like errorin order to add a red background border to the input.

问题是我不想error在输入上放置一个 css 类,也不想为输入添加红色背景边框。

How can I do this?

我怎样才能做到这一点?

回答by Madushan Perera

You can use if condition for check errors:

您可以使用 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>

回答by paulalexandru

This was the correct answer for me, without using additional div's:

这对我来说是正确的答案,没有使用额外的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')}}

回答by paulalexandru

you can use the following code to check if email field has Any validation error

您可以使用以下代码检查电子邮件字段是否有任何验证错误

 <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>

回答by Thomas Kim

You could just add HTML around it and style it however you want.

您可以在它周围添加 HTML 并根据需要设置样式。

HTML:

HTML:

<span class="error">{{$errors->first('email')}}</span>

CSS:

CSS:

.error {
    border: 1px solid red;
}

Edit: Add the has-errorclass to the input itself like this:

编辑:将has-error类添加到输入本身,如下所示:

{!! Form::email('email', null, array('placeholder' => 'Email', 'class' => 'form-control ' . $errors->first('email', 'has-error'))) !!}

回答by Abdulla Nilam

Add custom error message

添加自定义错误信息

'custom' => array(
    'email' => array(
        'required' => 'We need to know your e-mail address!',
    ),
),

Custom Error Messages in Laravel

Laravel 中的自定义错误信息

回答by Janko

<script> 
  if({{$errors->has('email')}}){
    $(input[type="email"]).css('border-color', 'red');
  }
</script>

回答by yudijohn

add class has-error to your form like this

像这样将类 has-error 添加到您的表单中

{!! Form::email('email', null, array('placeholder' => 'Email', 'class' => 'form-control has-error')) !!}

you can use if condition if there's an error, set the class in

如果出现错误,您可以使用 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

or you can do like this

或者你可以这样做

<input class="form-control {{ $errors->has('email') ? 'has-error' : '' }}" name="email" placeholder="Email">