Laravel:当它在那里时需要输入吗?

时间:2017-07-09 23:39:49

标签: php laravel

我试图在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');
        }
    }
}

3 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

不要在名称输入中使用点。

credentials.username替换为credentials_username

甚至更好,仅username

答案 2 :(得分:0)

在laravel验证中(。)表示项目数组。在这种情况下,您需要修改输入字段名称,如<input name="credentials[username]"> <input name="credentials[password]">

相关问题