这里,当我在一次用错误的数据填充字段时,此代码给出了错误,但旧的输入未显示。有什么解决办法吗?
我的控制器:-
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Userlist;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Validator;
class UserlistController extends Controller
{
public function index(){
return view('Project01.index');
}
public function store(Request $request){
$Validator = $request->validate([
'username' => 'required|unique:userlists,username|max:20',
'email' => 'required|email|unique:userlists,email|max:20',
'password' => 'required',
'bod' => 'required',
'comments' => 'required',
'Phone_no' => 'required',
'gender' => 'required',
'agreement' => 'required',
],[
'username.required' => 'Please Enter Your Username',
'email.required' => 'Please Enter Your Username',
'password.required' => 'Please Enter Your Username',
'bod.required' => 'Please Enter Your Username',
'comments.required' => 'Please Enter Your Username',
'Phone_no.required' => 'Please Enter Your Username',
]);
if($Validator->fails()){
return Redirect::back()->withErrors($Validator)->withInput();
}
}
}
Index.blade.php页面
@extends('Structure.main2')
@push('Extra-Header-Links')
<title>Project Index Page</title>
<style type="text/css">
.error-msg{
color: red !important;
}
</style>
@endpush
@section('main-content')
<div class="container">
<div class="col-lg-12 text-center mt-3 mb-3 border-bottom" >
<h1>Project01 Index</h1>
</div>
</div>
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-8 mb-5">
<form method="POST" action="{{ route('store') }}">
@csrf
<div class="form-group">
<label for="UserName"><b>UserName</b></label>
<input type="text" name="username" class="form-control" id="UserName" value="">
@error('username')
{{ $errors->first('username') }}
@enderror
</div>
<div class="form-group">
<label for="exampleInputEmail1"><b>Email address</b></label>
<input type="email" class="form-control" id="exampleInputEmail1" name="email" >
@if($errors->has('email'))
<small class="form-text text-muted error-msg"> {{ $errors->first('email') }} </small>
@endif
</div>
<div class="form-group">
<label for="exampleInputPassword1"><b>Password</b></label>
<input type="password" class="form-control" id="exampleInputPassword1" name="password">
</div>
<div class="form-group">
<label for="bod"><b>Birth Of Date</b></label>
<input type="date" class="form-control" id="bod" name="bod">
</div>
<div class="form-group">
<label for="comments"><b>Comments</b></label>
<textarea name="comments" class="form-control" id="comments" cols="30" rows="5"></textarea>
</div>
<div class="form-group">
<label for="Phone_no"><b>Phone No</b></label>
<input type="number" class="form-control" name="Phone_no" id="phone_no">
</div>
<div class="form-group">
<label ><b>Gender :- </b></label>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="gender" id="inlineradio1" value="option1" checked>
<label class="form-check-label" for="inlineradio1">Male</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="gender" id="inlineradio2" value="option2">
<label class="form-check-label" for="inlineradio2">Female</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="gender" id="inlineradio3" value="option3">
<label class="form-check-label" for="inlineradio3">Other</label>
</div>
</div>
<div class="form-group form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1" value="1" name="agreement">
<label class="form-check-label" for="exampleCheck1">Accept All Conditions</label>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
@endsection
@push('Extra-Footer-Scripts')
@endpush
路线:-
Route::prefix('project01')->group(function (){
Route::get('/', 'UserlistController@index');
Route::post('create', 'UserlistController@store')->name('store');
});
[请让我知道是否有人知道此问题的解决方案。我不知道为什么这对我不起作用。
$Validator = $request->validate([
'username' => 'required|unique:userlists,username|max:20',
'email' => 'required|email|unique:userlists,email|max:20',
'password' => 'required',
'bod' => 'required',
'comments' => 'required',
'Phone_no' => 'required',
'gender' => 'required',
'agreement' => 'required',
],[
'username.required' => 'Please Enter Your Username',
'email.required' => 'Please Enter Your Username',
'password.required' => 'Please Enter Your Username',
'bod.required' => 'Please Enter Your Username',
'comments.required' => 'Please Enter Your Username',
'Phone_no.required' => 'Please Enter Your Username',
]);
$validator->validate();
尝试一下