2015-11-05 33 views
2

我的驗證的形式工作正常,它看起來像這樣:Laravel 5驗證 - 在表單元素錯誤類

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

如果電子郵件不好,我得到這個錯誤:的電子郵件已被佔用。

事情是我不想在輸入上放一個css類,像error也是爲了給輸入添加一個紅色的背景邊框。

我該怎麼做?

+0

[此處輸入鏈接的描述(https://stackoverflow.com/questions/39637788/how-can -i-color-input-fields-border-red-when-i-get-validation-error-laravel-5-3/46314977#46314977)https://stackoverflow.com/questions/39637788/how-can-i -color-input-fields-border-red-when-i-get-validation-error-laravel-5-3/46314977#46314977 –

回答

3

這是正確的答案給我,而不使用額外的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')}} 
+0

這個問題是輸入不需要在Bootstrap中的.has-error類。這需要從父div應用。否則,這是一個很好的解決方案。 – Dev

0

添加它,但是你想自定義錯誤消息

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

Custom Error Messages in Laravel

+0

我不需要自定義錯誤消息。我需要添加一個css類到我的輸入。 – paulalexandru

+0

@paulalexandru [檢查此](http://stackoverflow.com/a/20352664/4595675) –

1

您可以只添加它周圍的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'))) !!} 
+0

我不需要創建一個新的元素。我需要將一個css類添加到我的電子郵件輸入中。 – paulalexandru

+0

有一個非常簡單的解決方案。 :)檢查我的編輯。 –

-1

附加類有錯誤到窗體像這樣

{!! 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"> 
+0

只有在表單驗證失敗的情況下,該類纔會出現,並不總是隨着您的擁有而出現。 – paulalexandru

+0

使用條件來跟蹤錯誤消息 – yudijohn

+0

@ paulalexandru檢查我編輯的代碼 – yudijohn

7

You can use if condition for check errors:

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

你可以用下面的代碼來檢查,如果電子郵件字段有任何驗證錯誤

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