我是Laravel的新手并且正在尝试验证表单的输入。我用刀片模板引擎。验证有效,因为它不会将数据插入数据库,但错误也不会显示出来。以下是我为SignUp表单所做的事情:
@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
<div class="row">
<div class="col-md-6">
<h3>Sign Up</h3>
<form action="{{ route('signup') }}" method="post">
<div class="form-group">
<label for="username">Username :</label>
<input class="form-control" type="text" name="username" id="username" >
</div>
<div class="form-group">
<label for="name">Name :</label>
<input class="form-control" type="text" name="name" id="name">
</div>
<div class="form-group">
<label for="email">E-Mail :</label>
<input class="form-control" type="text" name="email" id="email">
</div>
<div class="form-group">
<label for="password">Password :</label>
<input class="form-control" type="password" name="password" id="password">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
@endsection
此处是我的登录信息,我在其中验证字段并让用户登录:
public function postSignUp(Request $request)
{
$this->validate($request, [
'username' => 'required|unique:users|max:20|min:2',
'name' => 'required|max:30|min:2',
'email' => 'required|email|unique:users|max:50|min:5',
'password' => 'required|min:4'
]);
$username = $request["username"];
$name = $request["name"];
$email = $request["email"];
$password = bcrypt($request["password"]);
$user = new User();
$user->username = $username;
$user->name = $name;
$user->email = $email;
$user->password = $password;
$user->save();
Auth::login($user);
return redirect()->route('dashboard');
}
出于某种原因,当我执行已经使用的用户名或错误的电子邮件并单击“提交”按钮时,它会将我重定向回注册页面,这是预期的功能,但它不会显示错误所有
编辑:这是我的route.php:
Route::group(['middleware' => ['web']], function (){
Route::get('/', function () {
return view('welcome');
});
Route::post('/signup', [
'uses' => 'UserController@postSignUp',
'as' => 'signup'
]);
Route::post('/signin', [
'uses' => 'UserController@postSignIn',
'as' => 'signin'
]);
Route::get('/dashboard', [
'uses' => 'UserController@getDashboard',
'as' => 'dashboard'
]);
});
答案 0 :(得分:0)
您可以在刀片中尝试以下操作:
@section('content')
@if($errors->any())
<div class="row">
<div class="col-md-6">
<ul>
@foreach($errors->all() as $error)
<li>{{$error}}</li>
@endforeach
</ul>
</div>
</div>
@endif
<div class="row">
<div class="col-md-6">
<h3>Sign Up</h3>
<form action="{{ route('signup') }}" method="post">
<div class="form-group">
<label for="username">Username :</label>
<input class="form-control" type="text" name="username" id="username" >
</div>
<div class="form-group">
<label for="name">Name :</label>
<input class="form-control" type="text" name="name" id="name">
</div>
<div class="form-group">
<label for="email">E-Mail :</label>
<input class="form-control" type="text" name="email" id="email">
</div>
<div class="form-group">
<label for="password">Password :</label>
<input class="form-control" type="password" name="password" id="password">
</div>
<button type="submit" class="btn btn-primary">Submit</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
@endsection