为什么我在 Laravel 表单上的验证失败并路由到不同的页面?
Why is my validation on my Laravel form failing and routing to a different page?
我正在使用 Laravel 5.4 并尝试提交表单并检查输入是否为数字。但是当我进行验证时,它会路由到另一个 page.If 我删除验证然后一切正常。
这是我的 blade form:pin_verification.blade.php
@extends('layouts.master')
@section('title','Verify Order')
@section('extra_head_info')
<meta name="csrf-token" content="{{ csrf_token() }}">
@endsection
@section('content')
@if(count($errors) > 0)
<ul>
@foreach($errors->all() as $error)
<li class="alert alert-danger">{{$error}}</li>
@endforeach
</ul>
@endif
{{ Form::open(['action' => 'PinVerificationController@pinValidation','id'=>'pin_verification_form']) }}
We sent a text message to {{$clean_number}}. You should receive it within a few seconds.<br><br>
{{ Form::label('Pin Code', null, ['class' => 'control-label']) }}
{{ Form::text('pin_number', null,['placeholder' => 'Pin Code'])}}<br><br>
Enter a 4 digit pin you received by phone.
<br>
<br>
{{ Form::submit('Verify',['name'=>'validate'])}}
{{ Form::close() }}
@endsection
当我点击 pin_verification.blade.php 表单上的提交按钮时
我去我的 PinVerificationController.php:
class PinVerificationController extends Controller
{
public function pinValidation(Request $request){
if($request->has('validate')){
$validator = $this->validate($request,[
'pin_number' => 'required|numeric'
]);
return redirect("/test/create");
//return $this->validatePin($request);
}else{//choosing verification method
return $this->choosePinVerficationMethod($request);
}
}
public function init(){
...
}
public function choosePinVerficationMethod(Request $request){
...
}
}
我的代码进入 pinValidation 函数并进入第一个 if 语句,但是当它遇到
$validator = $this->validate($request,[
'pin_number' => 'required|numeric'
]);
它将我的 init() 函数路由到另一个控制器,CheckoutController.php
如果我删除我的验证,那么我的代码可以正常工作,我会被重定向到 redirect("/test/create");
为什么会这样?
我的路线是:
Route::get('/order/verify/{encrypted_key}', ['as'=>'pinverification','uses'=>'PinVerificationController@init']);
Route::get('/test/create', ['as'=>'orders_create', 'uses'=>'OrdersController@init']);
Route::post('/order/verify', ['as'=>'pinverification1', 'uses'=>'PinVerificationController@pinValidation']);
Route::get('/order/checkout/{product_id}', ['as'=>'checkout', 'uses'=>'CheckoutController@init']);
这是因为你实际上并没有检查验证,你只是在执行方法,所以自然地它只是按照第一个 return 重定向指令路由到 /test/create
。
试试这个:
public function pinValidation(Request $request){
if($request->has('validate')){
$validator = Validator::make($request->all(), [
'pin_number' => 'required|numeric'
]);
if ($validator->fails()) {
return redirect('test/create')
->withErrors($validator)
->withInput();
} else {
// Success criteria, validation passed.
}
} else {
$this->choosePinVerficationMethod($request);
}
}
validate方法,你在这里使用了重定向你自己:
$validator = $this->validate($request,[
'pin_number' => 'required|numeric'
]);
你应该使用:
$validator = Validator::make($request,[
'pin_number' => 'required|numeric'
]);
然后检查:
if ($validator->fails()) {
//redirect
};
我正在使用 Laravel 5.4 并尝试提交表单并检查输入是否为数字。但是当我进行验证时,它会路由到另一个 page.If 我删除验证然后一切正常。
这是我的 blade form:pin_verification.blade.php
@extends('layouts.master')
@section('title','Verify Order')
@section('extra_head_info')
<meta name="csrf-token" content="{{ csrf_token() }}">
@endsection
@section('content')
@if(count($errors) > 0)
<ul>
@foreach($errors->all() as $error)
<li class="alert alert-danger">{{$error}}</li>
@endforeach
</ul>
@endif
{{ Form::open(['action' => 'PinVerificationController@pinValidation','id'=>'pin_verification_form']) }}
We sent a text message to {{$clean_number}}. You should receive it within a few seconds.<br><br>
{{ Form::label('Pin Code', null, ['class' => 'control-label']) }}
{{ Form::text('pin_number', null,['placeholder' => 'Pin Code'])}}<br><br>
Enter a 4 digit pin you received by phone.
<br>
<br>
{{ Form::submit('Verify',['name'=>'validate'])}}
{{ Form::close() }}
@endsection
当我点击 pin_verification.blade.php 表单上的提交按钮时 我去我的 PinVerificationController.php:
class PinVerificationController extends Controller
{
public function pinValidation(Request $request){
if($request->has('validate')){
$validator = $this->validate($request,[
'pin_number' => 'required|numeric'
]);
return redirect("/test/create");
//return $this->validatePin($request);
}else{//choosing verification method
return $this->choosePinVerficationMethod($request);
}
}
public function init(){
...
}
public function choosePinVerficationMethod(Request $request){
...
}
}
我的代码进入 pinValidation 函数并进入第一个 if 语句,但是当它遇到
$validator = $this->validate($request,[
'pin_number' => 'required|numeric'
]);
它将我的 init() 函数路由到另一个控制器,CheckoutController.php
如果我删除我的验证,那么我的代码可以正常工作,我会被重定向到 redirect("/test/create");
为什么会这样?
我的路线是:
Route::get('/order/verify/{encrypted_key}', ['as'=>'pinverification','uses'=>'PinVerificationController@init']);
Route::get('/test/create', ['as'=>'orders_create', 'uses'=>'OrdersController@init']);
Route::post('/order/verify', ['as'=>'pinverification1', 'uses'=>'PinVerificationController@pinValidation']);
Route::get('/order/checkout/{product_id}', ['as'=>'checkout', 'uses'=>'CheckoutController@init']);
这是因为你实际上并没有检查验证,你只是在执行方法,所以自然地它只是按照第一个 return 重定向指令路由到 /test/create
。
试试这个:
public function pinValidation(Request $request){
if($request->has('validate')){
$validator = Validator::make($request->all(), [
'pin_number' => 'required|numeric'
]);
if ($validator->fails()) {
return redirect('test/create')
->withErrors($validator)
->withInput();
} else {
// Success criteria, validation passed.
}
} else {
$this->choosePinVerficationMethod($request);
}
}
validate方法,你在这里使用了重定向你自己:
$validator = $this->validate($request,[
'pin_number' => 'required|numeric'
]);
你应该使用:
$validator = Validator::make($request,[
'pin_number' => 'required|numeric'
]);
然后检查:
if ($validator->fails()) {
//redirect
};