【问题标题】:Laravel 5.2 validation erros are empty at random momentsLaravel 5.2 验证错误在随机时刻为空
【发布时间】:2016-06-17 22:00:23
【问题描述】:

我的 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]);
}

查看.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

【问题讨论】:

  • 没有一些代码很难说
  • 我将添加一些代码,但没有什么特别的事情发生。只是在将数据写入数据库的控制器中使用 store 方法的基本表单和基本验证。
  • 我用一些代码更新了我的原始帖子@DamienPirsy
  • 您在本地环境和生产环境中运行什么操作系统和服务器?你是怎么调试的?我可以推荐使用github.com/barryvdh/laravel-debugbar
  • @haakym 实时服务器上的操作系统是 Debian。在本地,我通过宅基地经营一切。我确实使用 Laravel 调试栏,但我不能在实时服务器上使用它,因为该项目已经启动。在我的本地和暂存环境中,我无法重现它。

标签: php forms validation laravel-5.2


【解决方案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') }} 

【讨论】:

  • 一切都在网络中间件中。
  • 您的代码看起来不错,您的路线也正常,您是否在本地和生产环境中使用相同的会话驱动程序?如果您使用的是文件驱动程序,它是否具有正确的权限?您的标题中没有任何重定向?或者您是否检查过您的会话配置文件是否使用“UTF-8 w/o BOM”编码。
猜你喜欢
  • 2023-04-01
  • 2016-03-28
  • 2016-08-17
  • 1970-01-01
  • 2023-04-03
  • 2015-11-09
  • 2017-07-01
  • 1970-01-01
  • 2016-10-31
相关资源
最近更新 更多