2016-03-04 77 views
4

我的Laravel應用程序驗證有一些非常奇怪的現象。我有一個應用程序,用戶只能通過url中的/ hers unique hash/code訪問它。當帶有散列的url匹配用戶時,我會用表單填寫用戶的個人資料信息。然後用戶必須使用表格確認/完成/修改信息。這工作正常,但是當提交表單時有時驗證不正常。Laravel 5.2驗證錯誤在隨機時刻是空的

例如,我留下了一些空的必填字段,我提交表單,我重定向回來,我的錯誤顯示在帶有漂亮紅色邊框的表單字段中。迄今爲止都很好。但是,由於某些未知的原因,有時在驗證所需的字段中提交空值的表單時,它會重新導回並顯示預填充的配置文件表單,但errors變量爲空,但驗證仍然失敗!

發生這種情況時,他們沒有任何線索,有時發生在第一次提交時,有時我必須在發生之前提交表單30次。現在我們用一個額外的前端驗證層來解決它,因爲應用程序必須上線,但我不能停止考慮發生這種情況的原因和方式。

我使用Request類進行驗證,但我也嘗試在我的控制器中創建一個手動驗證器,但它具有完全相同的行爲。我首先想到的是它與填寫表單有關,所以我試着在有錯誤的時候嘗試,並且我沒有預先填充任何東西(當然,除非是舊的輸入),但問題仍然存在。

它最奇怪的部分是錯誤是空的,但一些必填字段沒有填寫(和他們的名字是正確的),因爲問題並不總是會發生。我一直無法在本地和分段環境中重現問題,但它仍然在我的活動服務器上發生。

如果有人對我做錯了什麼或者我發生了什麼有任何建議,那將會很棒。我做了這1000次與其他時間的唯一區別是我預先填寫了表單,但是當我關閉該功能時我也有。

編輯:根據要求我的代碼如下。 注意:我替換了一些關鍵字,如路線,重定向和關係名稱。

Request類

<?php 

namespace App\Http\Requests; 

use App\Http\Requests\Request; 

class RegistrationRequest 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 [ 
      'hash'  => 'required|exists:users,hash', 
      'language' => 'required|in:NLD,FRA', 
      'title'  => 'required', 
      'firstName' => 'required', 
      'lastName' => 'required', 
      'street'  => 'required', 
      'postalCode' => 'required', 
      'city'  => 'required', 
      'email'  => 'required|email', 
      'birthday' => 'required|date_format:d/m/Y', 
      'tac'  => 'required' 
     ]; 
    } 
} 

控制器方法的索引。

public function index($hash) 
{ 
    $user = $this->user->byHash($hash); 

    if(is_null($user)) { 
     return redirect()->to('/'); 
    } 

    if(! is_null($user->myRelationName)) { 
     return redirect()->route('thanks'); 
    } 

    return view('my-view', ['user' => $user]); 
} 

控制方法商店

public function store(RegistrationRequest $request) 
{ 
    $user    = $this->user->byHash($request->hash); 
    $user->language = $request->language; 
    $user->title  = $request->title; 
    $user->firstName = $request->firstName; 
    $user->lastName = $request->lastName; 
    $user->street  = $request->street; 
    $user->postalCode = $request->postalCode; 
    $user->city  = $request->city; 
    $user->email  = $request->email; 
    $user->birthday = $request->birthday; 
    $user->tac  = true; 
    $user->ip   = $this->getRemoteIPAddress(); 
    $user->save(); 

    return redirect()->route('my-route', ['hash' => $request->hash]); 
} 

Vieuw.blade.php

@extends('layouts.master') 

@section('content') 


<div class="bottom"> 
    <div class="form-container"> 

     <h2>{{trans('merci.register_maintitle')}}</h2> 
     <p>{!!trans('merci.register_p1')!!}</p> 

     <p>{!!trans('merci.register_p2')!!}</p> 

     <h3>{!!trans('merci.register_h3')!!}</h3> 

     {{ Form::open(['route' => 'register.store', 'class' => 'form', 'id' => "register-form"]) }} 
      {{Form::hidden('hash', $user->hash, array("id" => "hash"))}} 

      <div class="form-field-wrap form-group language {{$errors->has('language') ? 'has-error' : null}}"> 
       {{ Form::label('language', trans('merci.register_language'), array('class' => 'form-field-text-label radio-label'))}} 

       {{ Form::radio('language', trans('merci.register_language1_value'), ($user->language == trans('merci.register_language1_value')) ? true : false, array('id' => 'nl-rad')) }} 
       <span>{{trans('merci.register_language1_label')}}</span> 

       {{ Form::radio('language', trans('merci.register_language2_value') , ($user->language == trans('merci.register_language2_value')) ? true : false, array('class' => 'radio', "id"=> 'fr-rad')) }} 
       <span>{{trans('merci.register_language2_label')}}</span> 
      </div> 

      <div class="form-field-wrap form-group title {{$errors->has('title') ? 'has-error' : null}}"> 

       {{ Form::label('title', trans('merci.register_title'), array('class' => 'form-field-text-label radio-label'))}} 

       {{ Form::radio('title', trans('merci.register_title1_value'), ($user->title == trans('merci.register_title1_value')) ? true : false) }} 
       <span>{{trans('merci.register_title1_label')}}</span> 

       {{ Form::radio('title', trans('merci.register_title2_value'), ($user->title == trans('merci.register_title2_value')) ? true : false, array('class' => 'radio')) }} 
       <span>{{trans('merci.register_title2_label')}}</span> 
      </div> 

      <div class="form-field-wrap form-group lastName {{$errors->has('lastName') ? 'has-error' : null}}"> 
       {{ Form::label('lastName', trans('merci.register_lastName'), array('id' => 'lastName', 'class' => 'form-field-text-label'))}} 

       {{ Form::text('lastName', $user->lastName, array('class' => 'form-field-text-input')) }} 
      </div> 

      <div class="form-field-wrap form-group firstName {{$errors->has('firstName') ? 'has-error' : null}}"> 
       {{ Form::label('firstName', trans('merci.register_firstName') , array('class' => 'form-field-text-label'))}} 

       {{ Form::text('firstName', $user->firstName, array('class' => 'form-field-text-input')) }} 
      </div> 

      <div class="extramargin form-field-wrap form-group street {{$errors->has('street') ? 'has-error' : null}}"> 
       {{ Form::label('street', trans('merci.register_street'), array('class' => 'form-field-text-label'))}} 

       {{ Form::text('street', $user->street, array('class' => 'form-field-text-input big')) }} 
      </div> 

      <div class="smallerpostal form-field-wrap form-group postalCode {{$errors->has('postalCode') ? 'has-error' : null}}"> 
       {{ Form::label('postalCode', trans('merci.register_postalCode'), array('class' => 'form-field-text-label smaller-label'))}} 

       {{ Form::text('postalCode', $user->postalCode, array('class' => 'form-field-text-input smaller')) }} 
      </div> 

      <div class="smallercity form-field-wrap form-group city {{$errors->has('city') ? 'has-error' : null}}"> 

       {{ Form::label('city', trans('merci.register_city'), array('class' => 'form-field-text-label smal-label'))}} 

       {{ Form::text('city', $user->city, array('class' => 'form-field-text-input smal')) }} 
      </div> 

      <div class="extramargin form-field-wrap form-group email {{$errors->has('email') ? 'has-error' : null}}"> 
       {{ Form::label('email', trans('merci.register_email'), array('class' => 'form-field-text-label'))}} 

       {{ Form::email('email', $user->email, array('class' => 'form-field-text-input ')) }} 
      </div> 

      <div class="form-field-wrap form-group birthday {{$errors->has('birthday') ? 'has-error' : null }} "> 
       {{ Form::label('birthday', trans('merci.register_birthday'), array('class' => 'form-field-text-label', "id" => "birthdate"))}} 

       {{ Form::text('birthday', $user->birthday, array('class' => 'form-field-text-input', "id"=>"datepicker")) }} 
      </div> 

      <div class="check form-field-wrap form-group tac {{$errors->has('tac') ? 'has-error' : null }}"> 
       {{ Form::checkbox('tac', trans('merci.register_tac_value'), false, array('class' => 'form-field-checkbox', "id" => "tac"))}} 
       {{ Form::label('tac', trans('merci.register_tac_label'), array('class' => 'form-field-error-label')) }} 
       <span>{!!trans('merci.register_tac_label_link')!!}</span> 
      </div> 

      @if (count($errors) > 0) 
      <div id="error server" style='display:none;'> 
      @else 
      <div id="error" style='display:none;'> 
      @endif 
       <p class="error">{{trans('merci.register_error')}}</p> 
      </div> 


      {!! Form::submit(trans('merci.register_submit'), array('class' => 'btn-main btn')) !!} 
     {{ Form::close() }} 
     <small>{{trans('merci.register_mandatory')}}</small> 

    </div> 
</div> 
<script src="{{ asset('js/validate.js') }}"></script> 
@stop 
+0

很難說沒有一些代碼 –

+0

我會添加一些代碼,但沒有什麼特別的是happing。只需在控制器中使用存儲方法進行基本表單和基本驗證,即將數據寫入數據庫。 –

+0

我用一些代碼更新了我的原始文章@DamienPirsy –

回答

-1

我不知道,如果你的路由是網絡中間件裏面,但如果你失去了$錯誤包變量,這可能是原因。

不放在網絡中間件組內的任何路線不會有 訪問會話和CSRF保護(見the default routes file)。

當$ errors變量被web中間件組綁定到視圖時,該變量將始終在您的視圖中可用。 (見Displaying The Validation Errors)。

// Make sure any routes that need access to session features are placed within 

Route::group(['middleware' => 'web'], function() { 

    Route::get('/', '[email protected]'); 
}); 

而且,我會使用舊助手在刀片模板從先前的請求檢索一閃而輸入。

{{ old('username') }} 
+0

所有內容都位於網絡中間件中。 –

+0

你的代碼看起來很不錯,你的路由可以,所以你是否使用同一個會話驅動程序進行本地和生產?如果您使用的是文件驅動程序,它是否具有正確的權限?你的頭文件中沒有任何重定向?或者是否檢查過會話配置文件是否使用「UTF-8 w/o BOM」進行編碼。 – userIx

+0

這一切都很好看... –