2017-07-09 18 views
1

私はLaravelでログインシステムをコード化しようとしており、テキストボックスが必要であることを教えています。私はテキストボックスを追加し、名前が正しいことを再確認し、私はそれがbelowsにフォームの提出時にテキストを入力することを確認しました。Laravel:入力が必要な場合は入力してください。

「credentials.usernameフィールドは必須です」というメッセージが表示され続けますが、検証から必要なものを削除すると、パスワードのためにそれも表示されます。

HTML:

<form method="post"> 
    <div id="login-columns"> 
     <div id="login-column-1"> 
      <label for="credentials-email">Username</label> 
      <input id="credentials-email" name="credentials.username" tabindex="2" type="text"> 
      <input id="credentials-remember-me" name="_login_remember_me" tabindex="5" type="checkbox"> 
      <label class="sub-label" for="credentials-remember-me">Keep me logged in</label> 
     </div> 
     <div id="login-column-2"> 
      <label for="credentials-password">Password</label> 
      <input id="credentials-password" name="credentials.password" tabindex="3" type="password"> 
     </div> 
     <input name="_token" type="hidden" value="{{ csrf_token() }}"> 
     <div id="login-column-3"> 
      <input style="margin: -10000px; position: absolute;" type="submit" value="Login"> <a class="button" href="#" id="credentials-submit" tabindex="4"><b></b><span>Login</span></a> 
     </div> 
     <div id="login-column-4"> 
      888 Online 
     </div> 
    </div> 
</form> 

はPHP:

public function onPost(Request $request) 
{ 
    $validator = Validator::make($request->all(), [ 
     'credentials.username' => 'required|exists:users', 
     'credentials.password' => 'required' 
    ]); 

    if ($validator->fails()) { 
     return Redirect::back()->withErrors($validator->messages()); 
    } 
    else { 
     if (!Auth::attempt(['username' => $request->input('credentials.username'), 'password' => $request->input('credentials-password')])) { 
      return Redirect::back()->withMessage('Failed Authentication')->withColor('danger'); 
     } 
     else { 
      $user = Auth::user(); 
      $user->save(); 

      return Redirect::to('/home'); 
     } 
    } 
} 

答えて

0

私はあなたが見ている問題はLaravelは "ネストされた属性" として 'credentials.username' を扱うことであると信じています。たとえば、 'layout.head'ビューをレンダリングする場合、レイアウトフォルダ内のhead.blade.phpファイルが自動的に検索されます。

私はこのケースでは、あなたがのような配列を渡していると仮定だと思う:

<input id="credentials-email" name="credentials[username]" tabindex="2" type="text"> 

<input id="credentials-password" name="credentials[password]" tabindex="3" type="password"> 

は、あなたが試してみました:

<input id="credentials-email" name="credentials_username" tabindex="2" type="text"> 

<input id="credentials-password" name="credentials_password" tabindex="3" type="password"> 

をLaravelの検証にネストされた属性の非常に簡単な言及がありますドキュメントページ:https://laravel.com/docs/5.4/validation

0

ドットインナーネーム入力を使用しないでください。

credentials.usernamecredentials_usernameに置き換えます。

さらに、ちょうどusernameです。

0

ラベリング検証ドット(。)項目の配列を表します。この場合、入力フィールド名は次のように変更する必要があります<input name="credentials[username]"> <input name="credentials[password]">

関連する問題