Laravel 5.3,当验证失败时会话不闪烁任何内容

问题描述 投票:0回答:1

嗨,我有以下表格:

        <form action="{{  route('postCustomerInfo') }}" method="post" class="shipping-form">
            {{ csrf_field() }}
            <input type="hidden" name="note" value="hidden_note_field">
            <input type="hidden" name="country_id" value="1">
                <div class="form-checkout">
                <div class="form-fields">
                    @if($errors->has('name'))<div class="input-box error" data-error="{{ $errors->first('name') }}">
                        @else<div class="input-box">
                    @endif
                        <input type="text" name="name" value={{ old('name') }}>
                        <label for="">name</label>
                    </div>
                    @if($errors->has('phone'))<div class="input-box error"  data-error="{{ $errors->first('phone') }}">
                        @else <div class="input-box">
                    @endif
                        <input type="text" name="phone" value={{ old('phone') }} onkeypress='return event.charCode >= 48 && event.charCode <= 57'>
                        <label for="">phone</label>
                    </div>
                    @if($errors->has('email'))<div class="input-box error"  data-error="{{ $errors->first('email') }}">
                        @else <div class="input-box">
                    @endif
                        <input type="text" name="email" value="{{ old('email') }}">
                        <label for="">email</label>
                    </div>
                    <div class="row">
                        <div class="col-xs-5 col-sm-5 col-md-6">
                            @if($errors->has('city'))<div class="input-box error" data-error="{{ $errors->first('city') }}">
                                @else<div class="input-box">
                            @endif
                                <input type="text" name="city" value="{{ old('city') }}">
                                <label for="">city</label>
                            </div>
                        </div>
                        <div class="col-xs-7 col-sm-7 col-md-6">
                            @if($errors->has('postal_code'))<div class="input-box error" data-error="{{ $errors->first('postal_code') }}">
                                @else<div class="input-box error" data-error="{{ $errors->first('postal_code') }}">
                            @endif
                                <input type="text" name="postal_code" value="{{ old('postal_code') }}">
                                <label for="">postal code</label>
                            </div>
                        </div>
                    </div>
                    @if($errors->has('address_line'))<div class="input-box error" data-error="{{ $errors->first('address_line') }}">
                         @else<div class="input-box error" data-error="{{ $errors->first('address_line') }}">
                    @endif
                        <input type="text" name="address_line" value={{ old('address_line') }}>
                        <label for="">address</label>
                    </div>
                </div>
            </div>
        </form>
        <a href="" class="btn action">Go to  <span>shipping</span></a>

我通过jquery提交表单

  $(document).ready(function () {
        $('.btn.action').on('click','',function (e) {
            e.preventDefault();
            var $form = $('form.shipping-form');
            $form.submit();
        });
    });

我的 FormRequest 类是:

class CustomerInformationRequest extends FormRequest {
/**
 * 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 [
        'name'          => 'required|min:5|max:50',
        'email'         => 'required|email|min:5|max:50',
        'phone'         => 'required|min:3|max:20',
        'country_id'    => 'required',
        'city'          => 'required|min:3',
        'postal_code'   => 'required|digits:4',
        'address_line'  => 'required|min:5|max:50',
        'note'          => ''
    ];
}

}

我在 StackOverflow 中读到了这个帖子:Laravel 5 输入旧为空但我不认为这实际上是我的问题。而且我的 {{ old('values') }} 仍然是空的,我不知道为什么?有什么想法吗?

php validation session laravel-5 input
1个回答
0
投票
/**
 * The application's global HTTP middleware stack.
 *
 * These middleware are run during every request to your application.
 *
 * @var array
 */
protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
];

/**
 * The application's route middleware groups.
 *
 * @var array
 */
protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
        \App\Http\Middleware\BeforeAutoTrimmer::class
    ],

    'api' => [
        'throttle:60,1',
        'bindings',
    ],
];

/**
 * The application's route middleware.
 *
 * These middleware may be assigned to groups or used individually.
 *
 * @var array
 */
protected $routeMiddleware = [
    'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'setCurrency'=>\App\Http\Middleware\SetCurrency::class,
    'checkout'=>\App\Http\Middleware\Checkout::class,
];}`.  

我补充了

\App\Http\Middleware\BeforeAutoTrimmer::class
谁关心修剪我所有的表格:
public function handle($request, Closure $next)
    {
        $request->merge(array_map('trim', $request->all()));
        return $next($request);
    }
, 这
'setCurrency'=>\App\Http\Middleware\SetCurrency::class
谁关心从数据库到会话设置货币:

`公共函数句柄($request, 闭包$next) { if(!Session::has('货币')) { $currency = 设置::where('setting_name', 'currency')->select('setting_value as code')->first(); $currencyData = 货币::where('code', $currency->code)->select('symbol_position', 'symbol', 'code')->first();

        Session::put('currency', $currencyData);
        Session::save();
        return $next($request);
    }
    return $next($request);}`

'checkout'=>\App\Http\Middleware\Checkout::class,
如果会话中的购物车是空的,谁愿意重定向我:

`公共函数句柄($request, Closure $next) {

    if(!Session::has('cart')){
        return redirect()->route('home');
    }
    return $next($request);
}`

但其余的我认为它们是 laravel 的默认设置,我没有碰它们

© www.soinside.com 2019 - 2024. All rights reserved.