簡體   English   中英

Laravel電子郵件驗證顯示錯誤消息

[英]Laravel email validation showing wrong message

我已經在控制器中為登錄表單編寫了一些驗證。

$validator = Validator::make(Input::all(), [
    'email'     => 'email|required',
    'password'  => 'required'
]);

// If validation fails
if($validator->fails()) {
    // Redirect to some place
    return Redirect::route('account-sign-in-get')->withErrors($validator)->withInput();
} else {
    // Attempt to sign in
}

在我看來,我有以下幾點:

 @if($errors->has('email'))
    <div class="alert alert-warning" role="alert">
      {{ $errors->first('email') }}
    </div>
 @endif

我輸入一個字符串(不是有效的電子郵件地址),然后在提交時,我看到一個錯誤,指出:“電子郵件必須是有效的電子郵件地址”

相反,我得到:“電子郵件字段為必填”

        {{ Form::open(['route' => 'account-sign-in-post', 'role' => 'form']) }}

                <div class="form-group">

                {{ Form::label('email', 'E-mail', ['for' => 'email', 'class' => 'sr-only ']) }}
                {{ Form::text('username', NULL, ['placeholder' => 'Email', 'id' => 'email', 'class' => 'form-control']) }}

                @if($errors->has('email'))
                    <div class="alert alert-warning" role="alert">{{ $errors->first('email') }}</div>
                @endif

            </div><!-- End of .form-group -->

            <div class="form-group">

                {{ Form::label('password', 'Password', ['for' => 'password', 'class' => 'sr-only']) }}
                {{ Form::password('password', ['placeholder' => 'Password', 'class' => 'form-control', 'id' => 'password']) }}

                @if($errors->has('password')) 
                    <div class="alert alert-warning" role="alert">{{ $errors->first('password') }}</div>
                @endif

            </div><!-- End of .form-group -->

            {{ Form::submit('Sign in', ['class' => 'btn btn-primary btn-block']) }}

        {{ Form::close() }}

謝謝您閱讀此篇。 任何幫助表示贊賞:)

錯誤的形式。

特別是在我們有用於用戶名的文本輸入的地方。

 {{ Form::text('username', NULL, ['placeholder' => 'Email', 'id' => 'email', 'class' => 'form-control']) }}

應該用於電子郵件:

 {{ Form::text('email', NULL, ['placeholder' => 'Email', 'id' => 'email', 'class' => 'form-control']) }}

@arkhamDev是正確的,首先您需要添加這些更改,然后按照以下步驟進行操作。

差異規則的順序在這里很重要。 你有下面

$validator = Validator::make(Input::all(), [
    'email'     => 'email|required',
    'password'  => 'required'
]);

請嘗試以下代碼

$validator = Validator::make(Input::all(), [
    'email'     => 'required|email',
    'password'  => 'required'
]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM