Laravel 表单仅针对日志文件中的某些字段返回错误
Laravel form returning errors only for some fields in log file
我有申请表。此表单有几个字段,例如 "name"、"email"、"country" 等
Laravel 只是重定向回 return 电子邮件字段的错误。比如The email field is required.
。对于其他一切,它只是刷新页面,在我的 laravel.log
文件中,我收到以下错误:
[2017-11-16 08:52:30] production.ERROR:
Illuminate\Validation\ValidationException: The given data failed to
pass validation. in
C:\xampp\htdocs\series\commend-me\CommendMe\vendor\laravel\framework\src\Illuminate\Foundation\Validation\ValidatesRequests.php:89
这是我的路线:
Route::post('/postapply', [
'uses' => '\CommendMe\Http\Controllers\ApplyController@postApply',
'as' => 'post.apply',
'middleware' => ['auth'],
]);
和我的控制器:
public function postApply(Request $request) {
$this->validate($request, [
'name' => 'required|alpha_num|max:40|min:2',
'country' => 'required|alpha_num|max:100|min:2',
'body' => 'required|string|max:2000',
'mature' => 'required',
'email' => 'required|email|max:255',
]);
dd("test");
在这种情况下,dd 不会 return "test"。此外,验证只会重定向回电子邮件字段的错误,如果电子邮件字段在控制器中被重命名为其他内容,它将返回到仅将错误发送到日志文件。
为什么会这样?
编辑:
按要求添加标记:
<form role="form" method="post" action="{{ route ('post.apply') }}">
<div class="submitArtSection">
<div class="submitRequired submitRequiredTitle">Required</div>
<div class="border-bot">
<input class="submitArtTitle" type="text" name="name" maxlength="39" placeholder="Name">
</div>
<div class="submitRequired submitRequiredTitle">Required</div>
<div class="border-bot">
<input class="submitArtTitle" type="text" maxlength="255" name="email" placeholder="Email">
</div>
<div class="submitRequired submitRequiredTitle">Required</div>
<div class="border-bot">
<input class="submitArtTitle" type="text" maxlength="255" name="country" placeholder="Country">
</div>
<div class="submitRequired submitRequiredTitle">Required</div>
<div>
<textarea name="body" style="height: 175px;" maxlength="1980" placeholder="Tell us about yourself..."></textarea>
</div>
</div>
<div class="submitArtSection submitArtCheckers">
<div class="submitRequired">Required</div>
<div>
<span class="submitArtCaption submitArtRestrictionsHeader">
Age
</span>
<span>
<label>
<input type="radio" name="mature" value="0">
Under 18
<span class="checkmark"></span>
</label>
<label>
<input type="radio" name="mature" value="1">
18+
<span class="checkmark"></span>
</label>
</span>
</div>
</div>
<div class="text-center">
<button id="saveImage" class="pointer">Submit</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
dd($request->all());在方法的开头 return 以下
array:5 [▼
"name" => ""
"email" => ""
"country" => ""
"body" => ""
"_token" => "HKJ51CMvteYOD0OG79SQiRjpeTup9NNWvTfw1IWB"
]
尝试将输入类型更改为电子邮件而不是文本。您可能在提交时输入了文本值,Laravel 已经验证了电子邮件类型
<div class="border-bot">
<input class="submitArtTitle" type="text" maxlength="255" name="email" placeholder="Email">
</div>
到
<div class="border-bot">
<input class="submitArtTitle" type="email" maxlength="255" name="email" placeholder="Email">
</div>
我没有在我的标记中循环错误。
@if (count($errors) > 0)
<div class="alert alert-info" role="alert">
{{ $errors->first() }}
<a href="#" class="alert-close" data-dismiss="alert" aria-label="close">×</a>
</div>
@endif
我之所以没有意识到这一点,是因为我的模板页面也有一个名为 email 的电子邮件字段(用于登录),并且循环出现错误。因此,只有同名的表单电子邮件才会重定向错误。
我有申请表。此表单有几个字段,例如 "name"、"email"、"country" 等
Laravel 只是重定向回 return 电子邮件字段的错误。比如The email field is required.
。对于其他一切,它只是刷新页面,在我的 laravel.log
文件中,我收到以下错误:
[2017-11-16 08:52:30] production.ERROR: Illuminate\Validation\ValidationException: The given data failed to pass validation. in C:\xampp\htdocs\series\commend-me\CommendMe\vendor\laravel\framework\src\Illuminate\Foundation\Validation\ValidatesRequests.php:89
这是我的路线:
Route::post('/postapply', [
'uses' => '\CommendMe\Http\Controllers\ApplyController@postApply',
'as' => 'post.apply',
'middleware' => ['auth'],
]);
和我的控制器:
public function postApply(Request $request) {
$this->validate($request, [
'name' => 'required|alpha_num|max:40|min:2',
'country' => 'required|alpha_num|max:100|min:2',
'body' => 'required|string|max:2000',
'mature' => 'required',
'email' => 'required|email|max:255',
]);
dd("test");
在这种情况下,dd 不会 return "test"。此外,验证只会重定向回电子邮件字段的错误,如果电子邮件字段在控制器中被重命名为其他内容,它将返回到仅将错误发送到日志文件。
为什么会这样?
编辑:
按要求添加标记:
<form role="form" method="post" action="{{ route ('post.apply') }}">
<div class="submitArtSection">
<div class="submitRequired submitRequiredTitle">Required</div>
<div class="border-bot">
<input class="submitArtTitle" type="text" name="name" maxlength="39" placeholder="Name">
</div>
<div class="submitRequired submitRequiredTitle">Required</div>
<div class="border-bot">
<input class="submitArtTitle" type="text" maxlength="255" name="email" placeholder="Email">
</div>
<div class="submitRequired submitRequiredTitle">Required</div>
<div class="border-bot">
<input class="submitArtTitle" type="text" maxlength="255" name="country" placeholder="Country">
</div>
<div class="submitRequired submitRequiredTitle">Required</div>
<div>
<textarea name="body" style="height: 175px;" maxlength="1980" placeholder="Tell us about yourself..."></textarea>
</div>
</div>
<div class="submitArtSection submitArtCheckers">
<div class="submitRequired">Required</div>
<div>
<span class="submitArtCaption submitArtRestrictionsHeader">
Age
</span>
<span>
<label>
<input type="radio" name="mature" value="0">
Under 18
<span class="checkmark"></span>
</label>
<label>
<input type="radio" name="mature" value="1">
18+
<span class="checkmark"></span>
</label>
</span>
</div>
</div>
<div class="text-center">
<button id="saveImage" class="pointer">Submit</button>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>
dd($request->all());在方法的开头 return 以下
array:5 [▼
"name" => ""
"email" => ""
"country" => ""
"body" => ""
"_token" => "HKJ51CMvteYOD0OG79SQiRjpeTup9NNWvTfw1IWB"
]
尝试将输入类型更改为电子邮件而不是文本。您可能在提交时输入了文本值,Laravel 已经验证了电子邮件类型
<div class="border-bot">
<input class="submitArtTitle" type="text" maxlength="255" name="email" placeholder="Email">
</div>
到
<div class="border-bot">
<input class="submitArtTitle" type="email" maxlength="255" name="email" placeholder="Email">
</div>
我没有在我的标记中循环错误。
@if (count($errors) > 0)
<div class="alert alert-info" role="alert">
{{ $errors->first() }}
<a href="#" class="alert-close" data-dismiss="alert" aria-label="close">×</a>
</div>
@endif
我之所以没有意识到这一点,是因为我的模板页面也有一个名为 email 的电子邮件字段(用于登录),并且循环出现错误。因此,只有同名的表单电子邮件才会重定向错误。