Laravel只为日志文件中的某些字段返回错误

时间:2017-11-16 08:57:48

标签: php laravel laravel-5.3

我有一份申请表。此表单包含一些字段,例如“名称”,“电子邮件”,“国家/地区”等。

Laravel仅重定向返回电子邮件字段的错误。例如The email field is required.。对于其他所有内容,它只是刷新页面,在我的laravel.log文件中,我收到以下错误:

  

[2017-11-16 08:52:30] production.ERROR:   Illuminate \ Validation \ ValidationException:给定数据失败   通过验证。在   C:\ XAMPP \ htdocs中\系列\称道-ME \ CommendMe \厂商\ laravel \框架\ SRC \照亮\基金会\验证\ 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不会返回“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($请求 - &GT;所有());在方法的开头返回以下

array:5 [▼
  "name" => ""
  "email" => ""
  "country" => ""
  "body" => ""
  "_token" => "HKJ51CMvteYOD0OG79SQiRjpeTup9NNWvTfw1IWB"
]

2 个答案:

答案 0 :(得分:0)

尝试将输入的类型更改为电子邮件而不是文本。您可能在提交时输入了文本值,Laravel已经对电子邮件类型进行了验证

<div class="border-bot">
            <input class="submitArtTitle" type="text" maxlength="255" name="email"  placeholder="Email">
        </div>     

TO

<div class="border-bot">
            <input class="submitArtTitle" type="email" maxlength="255" name="email"  placeholder="Email">
        </div>  

答案 1 :(得分:0)

我的标记中没有出现错误。

@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(用于登录)的电子邮件字段,并且正在为它循环错误。因此,只有表单的电子邮件具有相同的名称,才会重定向错误。