Laravel 5.2验证错误在随机时刻是空的

时间:2016-03-04 08:39:39

标签: php forms validation laravel-5.2

我的Laravel应用程序的验证有一些非常奇怪的事情。我有一个应用程序,用户只能用他/她在url中的唯一哈希/代码访问它。当具有哈希的url与用户匹配时,我使用用户的配置文件信息预填充表单。然后,用户必须使用表单确认/完成/修改信息。这样可以正常工作,但在提交表单时有时验证不正常。

例如,我将一些字段留空是必需的并且我提交表单,我被重定向回来,我的错误显示在带有漂亮红色边框的表单字段中。到目前为止都很好。但是,由于某些未知原因,有时在验证所需的字段中提交具有空值的表单时,它会重定向并显示预填充的配置文件表单,但错误变量为空,但验证仍然失败!

当发生这种情况时,它们不会被吸引,有时它会在第一次提交时发生,有时我必须在它发生之前提交表格30次。现在我们通过额外的前端验证来解决它,因为应用程序必须上线,但我不能停止思考为什么以及如何发生这种情况。

我正在使用Request类进行验证,但我也尝试在我的控制器中创建一个手动验证器,但这有完全相同的行为。我首先想到它与预填充表格有关,所以我尝试了当有错误而且我没有预填充任何东西(当然除了输入旧),但问题仍然存在。

最奇怪的部分是错误是空的,但是一些必填字段没有填写(并且它们的名称是正确的),因为问题并不总是发生。我无法在本地和暂存环境中重现问题,但它仍然在我的实时服务器上发生。

如果有人对我做错了什么或我发生了什么有任何建议,那就太好了。我这样做了1000次与其他时间唯一不同的是我预先填写了表格,但是当我关闭这个功能时我也有它。

编辑按要求提供下面的代码。 注意:我替换了一些关键字,如路线,重定向和关系名称。

请求类

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

1 个答案:

答案 0 :(得分:-1)

我不知道你的路由是否在网络中间件内,但是如果你丢失了$ errors包变量,那可能就是原因。

  

未放置在Web中间件组中的任何路由都不会   访问会话和CSRF保护(参见the default routes file)。

当Web中间件组将$ errors变量绑定到视图时,此变量将始终在您的视图中可用。 (见Displaying The Validation Errors)。

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

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

    Route::get('/', 'MyController@index');
});

另外,我会使用刀片模板中的旧助手来检索上一个请求中的闪存输入。

{{ old('username') }} 
相关问题