2015-04-05 61 views
1

我已經創造了L5的請求來處理保存的接觸,它看起來像這樣單獨的驗證錯誤:Laravel 5的要求 - 如何顯示

<?php namespace App\Http\Requests; 

use App\Http\Requests\Request; 

class AddContactRequest extends Request { 

    /** 
    * Determine if the user is authorized to make this request. 
    * 
    * @return bool 
    */ 
    public function authorize() 
    { 
     return true; 
    } 

    /** 
    * Get the validation rules that apply to the request. 
    * 
    * @return array 
    */ 
    public function rules() 
    { 
     return [ 
      'category_ids' => 'required|array|exists:categories,id', 
      'gender' => 'required|in:Male,Female', 
      'first_name' => 'required', 
      'last_name' => 'required', 
      'company' => 'required', 
      'position' => 'required', 
      'website_url' => 'url', 
      'facebook_url' => 'url', 
      'twitter_url' => 'url', 
      'instagram_url' => 'url', 
      'misc_url1' => 'url', 
      'misc_url2' => 'url', 
      'address_1' => 'required', 
      'citytown' => 'required', 
      'postcode' => 'required', 
      'country_id' => 'required|integer|exists:countries,id', 
      'work_email' => 'email', 
      'home_email' => 'email', 
      'work_phone' => '', 
      'mobile_phone' => '', 
      'home_phone' => '' 
     ]; 
    } 

    /** 
    * Error messages for validation rules 
    * @return array 
    */ 
    public function messages() 
    { 
     return [ 
      'category_ids.required' => 'You must select at least one category.', 
      'category_ids.array' => 'Categories must be an array.', 
      'category_ids.exists' => 'The category does not exist.', 
      'gender.required' => 'The gender field is required.', 
      'gender.in' => 'The gender is invalid.', 
      'first_name.required' => 'The first name field is required.', 
      'last_name.required' => 'The last name field is required.', 
      'company.required' => 'The company field is required.', 
      'position.required' => 'The position field is required.', 
      'website_url.url' => 'The website URL is not a valid URL.', 
      'facebook_url.url' => 'The facebook URL is not a valid URL.', 
      'twitter_url.url' => 'The twitter URL is not a valid URL.', 
      'instagram_url.url' => 'The instagram URL is not a valid URL.', 
      'misc_url1.url' => 'The miscellaneous URL 1 field is not a valid URL', 
      'misc_url2.url' => 'The miscellaneous URL 2 field is not a valid URL', 
      'address_1.required' => 'The address 1 field is required.', 
      'citytown.required' => 'The city/town field is required.', 
      'postcode.required' => 'The postcode field is required.', 
      'country_id.required' => 'The country field is required.', 
      'country_id.integer' => 'The country field must contain an integer.', 
      'country_id.exists' => 'The country is invalid.', 
      'work_email.email' => 'The work email field is not a valid email address.', 
      'home_email.email' => 'The home email field is not a valid email address.' 
     ]; 
    } 

} 

如果驗證失敗,我能輸出所有的我用下面的代碼視圖錯誤:

@if (count($errors) > 0) 
    <div class="alert alert-danger"> 
     <strong>Whoops!</strong> There were some problems with your input.<br><br> 
     <ul> 
      @foreach ($errors->all() as $error) 
       <li>{{ $error }}</li> 
      @endforeach 
     </ul> 
    </div> 
@endif 

但我想說明個人的錯誤,並添加一個類的形式組如果該字段有像這樣的錯誤:

<div class="form-group <?php echo ($messages->has('first_name')) ? 'has-error' : ''; ?>"> 
    <label for="first_name" class="control-label col-xs-12 col-sm-3 col-md-4 col-lg-4">First Name</label> 
    <div class="col-xs-12 col-sm-6 col-md-8 col-lg-8"> 
     {!! Form::text('first_name', '', array('class' => 'form-control')) !!} 
    </div> 
</div> 

的$消息 - >有()方法似乎並不工作,但我得到以下錯誤:

Whoops, looks like something went wrong. 

2/2 
ErrorException in 6351f2a902813f4fd7aa70b7ef38354d line 34: 
Undefined variable: messages (View: C:\xampp\htdocs\fourteen-ten\resources\views\admin\contacts\add.blade.php) 

有誰知道爲什麼我收到此錯誤?

回答

2

OK感謝Sulthan我能弄明白。相反的:

$messages->has('field_name') 

我應該使用:

$errors->has('field_name') 
+0

是的,那太好了。 – 2015-04-05 11:39:55

8

單獨顯示錯誤。你應該把這個每項要素下

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

注:

您應該創建一個命名爲error在你的CSS這個類。

這將顯示錯誤,只有當任何occcurs

+0

這個效果很好,但最好使用條件(如果其他)爲顯示錯誤容器。 – 2018-02-17 17:55:17

0

我覺得這是一個簡單的過程:

$rules=array('mobile'=>'required|numeric'); 
$validator=Validator::make(Input::all(),$rules); 

if($validator->fails()){ 
    $failedRules=$validator->failed(); 
    if(isset($failedRules['signInType']['Required'])){ 
     return response()->json(["status"=>"Error","message"=>"Please enter signInType"]); 
    } 

    if(isset($failedRules['signInType']['Numeric'])){ 
     return response()->json(["status"=>"Error","message"=>"mobile must be numeric"]); 
    } 
} 
+0

對不起!我以這種方式發佈了代碼,因爲我不知道如何發佈評論 – 2016-09-09 11:40:55

+0

您需要[50聲望評論](http://stackoverflow.com/help/privileges/comment)。因此,如果有人讚揚你的貢獻,你可以提問和回答問題並贏得聲望。 – Alsatian 2016-09-09 14:39:52