dousuize3082
dousuize3082
2017-06-01 10:22
浏览 24
已采纳

注册表单验证问题

I'm adding a validation to my sign up form , the validate code is working but the errors don't shows up in the page

PHP code

 public function postSignUp(Request $request)
{
        $this->validate($request, [
            'email'=> 'required|email|unique:users',
            'first_name'=> 'required|max:120',
            'password' => 'required|min:4'
            ]);

        $email = $request['email'];
        $first_name = $request['first_name'];
        $password = bcrypt($request['password']);

        $user =  new User();

        $user->email = $email;
        $user->first_name = $first_name;
        $user->password = $password;

        $user->save();

        Auth::Login($user);

        return redirect()->route('dashboard');
}

Welcome.blade.php code

@section('content')
@if(count($errors) > 0)
<div class="row">
    <div class="col-md-6">
        <ul>
            @foreach($errors->all() as $error)
                <li>{{$error}}</li>
                @endforeach
        </ul>
    </div>
</div>
@endif

the php code is working because if i tried to register with an email that already taken it wont register but the error is that errors are not showing up in the welcome page

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongre8505
    dongre8505 2017-06-01 10:41
    已采纳

    You are not passing the error messages back to the view. As stated at documentation

        if ($validator->fails())
        {
            return redirect('dashboard')->withErrors($validator);
        }
    
    点赞 评论
  • dongqiang4819
    dongqiang4819 2017-06-01 10:32

    You are checking with the function count($errors) but that is an object and it will always enter the condition since you are counting the object not the list itself.

    Replace @if(count($errors) > 0) with @if(!empty($errors->all()))

    点赞 评论

相关推荐