2016-02-11 49 views
0

我試圖在Laravel 5上使用自定義請求來驗證通過發佈發送的輸入,但是當表單字段正常時,即。表單驗證正常,我的控制器的方法沒有被調用。這是一個錯誤?怎麼了?我知道這是可能的,因爲我曾經在另一個項目中這樣做過。我在這個項目中使用的是5.2.6版本,然後我認爲這是這個特定版本的問題,那是當我「切換回」到5.1,但編輯我的composer.json,刪除vendor文件夾,然後重新安裝與composer install自定義請求不調用表單驗證成功的控制器方法

我UserController中的內容:

//... 
public function save(Requests\CreateUserRequest $request) 
{ 
    $user = new User(); 

    $user->name = $request->input('name'); 
    $user->email = $request->input('email'); 
    $user->username = $request->input('username'); 
    $user->password = $request->input('password_confirmation'); 
    //$user->profile_id = $request->input('profile'); 
    $user->profile_id = 1; 

    $user->save(); 

    return redirect()->route('get_user_read'); 
} 
//... 

CreateUserRequest:

<?php 

namespace App\Http\Requests; 

use App\Http\Requests\Request; 

class CreateUserRequest 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 [ 
     'profile' => 'required', 
     'name' => 'required', 
     'username' => 'required|unique:users,username', 
     'email' => 'required|email|unique:users,email', 
     'password' => 'required', 
     'password_confirmation' => 'required|same:password', 
    ]; 
} 

public function messages() 
{ 
    return [ 
     'profile.required' => 'O campo perfil é obrigatório!', 
     'name.required' => 'O campo nome é obrigatório!', 
     'username.required' => 'O campo nome de usuário é obrigatório!', 
     'username.unique' => 'O nome de usuário já foi registrado!', 
     'email.required' => 'O campo email é obrigatório!', 
     'email.unique' => 'O email informado já foi registrado!', 
     'email.email' => 'O email informado não é válido!', 
     'password.required' => 'O campo senha é obrigatório!', 
     'password_confirmation.required' => 'O campo de confirmação de senha é obrigatório!', 
     'password_confirmation.same' => 'A confirmação de senha não confere com a senha informada!', 
    ]; 
} 
} 

感謝。

+0

請檢查「Requests \ CreateUserRequest」是否是您的請求文件的真實路徑 – BKF

+0

因此,如果您在save函數中使用dd()請求,什麼都不會發生? –

+0

爲了測試我的保存函數是否曾經被調用,我把'''return「」;'''放在其他所有內容之前;並且它從未被執行過。我應該看到一個空白頁面。正如我上面所說的,當我使用我的自定義請求來驗證表單輸入時,我的保存函數從不被調用。 –

回答

0

由於Laravel 5.2有一些名爲Middleware Groups的功能。

看起來它會讓您重定向回您的表格,因爲FAILED驗證不會顯示任何錯誤。除非您在會話中啓用分享,否則您不會看到任何錯誤。

要使錯誤可見,請確保\Illuminate\View\Middleware\ShareErrorsFromSession::class中間件已啓用。默認情況下,它包含在「web」中間件組中。

解決您的問題,您可以簡單地創建一個路由組,並設置一箇中間件集團以「網」是這樣的:

Route::group(['middleware' => ['web']], function() { 
    // Here comes your routes 
}); 

請把這個一看:Laravel 5.2 validation errors

還要確保你有在窗體刀片錯誤標籤是這樣的:

{!! Form::text('profile') !!} 
@if ($errors->has('profile'))<p style="color:red;">{!!$errors->first('profile')!!}</p>@endif 
{!! Form::text('name') !!} 
@if ($errors->has('name'))<p style="color:red;">{!!$errors->first('name')!!}</p>@endif 

也可以顯示所有的人:

@if($errors->any()) 
    <div style="color:red;"> 
     @foreach($errors->all() as $error) 
      <p>{{$error}}</p> 
     @endforeach 
    </div> 
@endif 

你也可以不使用public function messages()方法和定義所有使用resources/lang/pt/validation.php文件語言的消息。