2015-04-19 75 views
0

在引發驗證錯誤的表單上輸入數據時,不會重新輸入用戶以前的輸入。Laravel 5 - 表單上的驗證錯誤不會重新輸入用戶輸入

控制器

public function store(ClinicFormRequest $request) 
    { 

     $user = new \App\User; 
     $user->name = $request->name; 
     $user->email = $request->email; 
     $user->password = Hash::make(str_random(10)); 
     $user->save(); 

     $user->user_id; 

     $clinic = new \App\Clinic; 
     $clinic->name = $request->clinicname; 
     $clinic->telephone = $request->telephone; 
     $clinic->save(); 

     $clinic->users()->attach(user_id); 

     return Redirect::route('clinic.index')->with('message', 'Clinic created'); 

    } 

診所形式Reqeust:

class ClinicFormRequest extends Request { 

    public function authorize() 
    { 
     return true; 
    } 

    public function rules() 
    { 
     return [ 
     'clinicname' => 'required', 
     'name' => 'required', 
     'email' => 'required|email|unique:users', 
     'telephone' => 'required', 
     'address' => 'required', 
     'city' => 'required', 
     'postcode' => 'required' 
     ]; 
    } 

} 

Create.blade.php

@if($errors->has()) 
    <div class="alert alert-danger"> 
     @foreach($errors->all() as $error) 
      <li>{{$error}}</li> 
     @endforeach 
    </div> 
@endif 

{!! Form::model(new App\Clinic, ['route' => ['clinic.store'], "class" => "form-horizontal"]) !!} 

<fieldset> 

    <!-- Form Name --> 
    <legend>Add your clinic</legend> 

    <!-- Text input--> 
    <div class="form-group"> 
     <label class="col-md-4 control-label" for="clinicname">Clinic Name</label> 
     <div class="col-md-4"> 
      <input id="clinicname" name="clinicname" type="text" placeholder="" class="form-control input-md" required=""> 
      <span class="help-block">Your clinic's name</span> 
     </div> 
    </div> 

     <!-- Button --> 
     <div class="form-group"> 
      <label class="col-md-4 control-label" for="register">Register</label> 
      <div class="col-md-4"> 
       <button id="register" name="register" class="btn btn-success">Register your clinic</button> 
      </div> 
     </div> 

    </fieldset> 

    {!! Form::close() !!} 

routes.php文件

Route::resource('clinic', 'ClinicController'); 

任何幫助將不勝感激。非常感謝。

+0

您是否嘗試過[方法](http://laravel.com/docs/5.0/requests#old-input)? –

+0

爲什麼你要調用'$ user-> user_id'? –

+0

@GeorgeBoot,因爲我需要將診所分配給用戶。檢查'controller'中的倒數第二行。它存儲在數據透視表中。 – Ben

回答

3

您需要從舊的輸入搶值,就像這樣:

<!-- Text input--> 
<div class="form-group"> 
    <label class="col-md-4 control-label" for="clinicname">Clinic Name</label> 
    <div class="col-md-4"> 
     <input id="clinicname" name="clinicname" type="text" placeholder="" class="form-control input-md" required="" value="{{ old('clinicname') }}"> 
     <span class="help-block">Your clinic's name</span> 
    </div> 
</div> 

你也應該考慮使用illuminate/html包,這個自動檢索舊的輸入你的過程。