发送错误消息 laravel 需要输入
sending error message laravel required inputs
所以我想验证我的注册表单,所以我做了所有必需的输入,这样我就可以测试我的验证是否有效,所以这就是我在我的控制器中使用的
$Message = [
'required' => 'This input is required',
];
$validator = Validator::make($request->all(), [
'name' => 'required',
'email' => 'required',
'password' => 'required',
'UserName' => 'required',
'Phone' => 'required',
'SSN' => 'required',
'SDT' => 'required',
'Country' => 'required',
'InsuranceAmount' => 'required',
'City' => 'required',
'Location' => 'required'
], $Message);
if ($validator->fails()) {
return redirect('/admin/users/create')
->withErrors($validator)
->withInput();
}
$user = new User;
$user->name = Input::get('name');
$user->email = Input::get('email');
$user->password = bcrypt(Input::get('password'));
$user->UserName = input::get('UserName');
$user->Phone = input::get('Phone');
$user->SSN = input::get('SSN');
$user->SDT = input::get('SDT');
$user->Country = input::get('Country');
$user->City = input::get('City');
$user->Location = input::get('Location');
$user->save();
return Redirect::to('/admin/users')->with('message', 'User Created');
现在,如果没有错误,它可以正常工作并重定向到用户列表,但如果输入为空,它只会重定向到我想要的创建页面,但问题是它不会发送错误消息通过重定向,我尝试了 dd 验证器,它的所有消息都很好,这是我的观点
<form class="form-horizontal" role="form" method="POST" action="{{ Route('userstore') }}">
{!! csrf_field() !!}
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">الاسم</label>
<div class="col-md-6">
<input type="text" class="form-control" name="name" value="{{ old('name') }}">
@if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group {{$errors->has('UserName') ? 'has-error' : ''}}">
<label class="col-md-4 control-label">اسم المستخدم</label>
<div class="col-md-6">
<input type="text" class="form-control" name="UserName" value="{{ old('UserName') }}">
@if ($errors->has('UserName'))
<span class="help-block">
<strong>{{ $errors->first('UserName') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group {{$errors->has('Phone') ? 'has-error' : ''}}">
<label class="col-md-4 control-label">رقم الجوال</label>
<div class="col-md-6">
<input type="text" class="form-control" name="Phone" value="{{old('Phone')}}">
@if ($errors->has('Phone'))
<span class="help-block">
<strong>{{ $errors->first('Phone') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group {{$errors->has('SSN') ? 'has-error' : ''}}">
<label class="col-md-4 control-label">الرقم الوطني</label>
<div class="col-md-6">
<input type="text" class="form-control" name="SSN" value="{{old('SSN')}}">
@if ($errors->has('SSN'))
<span class="help-block">
<strong>{{ $errors->first('SSN') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group {{$errors->has('SDT') ? 'has-error' : ''}}">
<label class="col-md-4 control-label">نوع الوثيقة</label>
<div class="col-md-6">
<input type="text" class="form-control" name="SDT" value="{{old('SDT')}}">
@if ($errors->has('SDT'))
<span class="help-block">
<strong>{{ $errors->first('SDT') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group {{$errors->has('Country') ? 'has-error' : ''}}">
<label class="col-md-4 control-label">الدولة</label>
<div class="col-md-6">
<select class="form-control" name="Country">
<option>الاردن</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">المدينة</label>
<div class="col-md-6">
<select class="form-control" name="City" >
<option>عمان</option>
</select>
</div>
</div>
<div class="form-group {{$errors->has('Location') ? 'has-error' : ''}}">
<label class="col-md-4 control-label">اسم الشارع</label>
<div class="col-md-6">
<input type="text" class="form-control" name="Location" value="{{old('Location')}}">
@if ($errors->has('Location'))
<span class="help-block">
<strong>{{ $errors->first('Location') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }} ltr-input">
<label class="col-md-4 control-label">البريد الإلكتروني</label>
<div class="col-md-6">
<input type="email" class="form-control" name="email" value="{{ old('email') }}">
@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }} ltr-input">
<label class="col-md-4 control-label">كلمة المرور</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password">
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }} ltr-input">
<label class="col-md-4 control-label">تأكيد كلمة المرور</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password_confirmation">
@if ($errors->has('password_confirmation'))
<span class="help-block">
<strong>{{ $errors->first('password_confirmation') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary pull-right">
<i class="fa fa-btn fa-user"></i> تسجيل
</button>
</div>
</div>
</form>
顺便说一句,这是 laravel 5.1
我通过将我的项目从 laravel 5.1 升级到 5.5
来解决问题
所以我想验证我的注册表单,所以我做了所有必需的输入,这样我就可以测试我的验证是否有效,所以这就是我在我的控制器中使用的
$Message = [
'required' => 'This input is required',
];
$validator = Validator::make($request->all(), [
'name' => 'required',
'email' => 'required',
'password' => 'required',
'UserName' => 'required',
'Phone' => 'required',
'SSN' => 'required',
'SDT' => 'required',
'Country' => 'required',
'InsuranceAmount' => 'required',
'City' => 'required',
'Location' => 'required'
], $Message);
if ($validator->fails()) {
return redirect('/admin/users/create')
->withErrors($validator)
->withInput();
}
$user = new User;
$user->name = Input::get('name');
$user->email = Input::get('email');
$user->password = bcrypt(Input::get('password'));
$user->UserName = input::get('UserName');
$user->Phone = input::get('Phone');
$user->SSN = input::get('SSN');
$user->SDT = input::get('SDT');
$user->Country = input::get('Country');
$user->City = input::get('City');
$user->Location = input::get('Location');
$user->save();
return Redirect::to('/admin/users')->with('message', 'User Created');
现在,如果没有错误,它可以正常工作并重定向到用户列表,但如果输入为空,它只会重定向到我想要的创建页面,但问题是它不会发送错误消息通过重定向,我尝试了 dd 验证器,它的所有消息都很好,这是我的观点
<form class="form-horizontal" role="form" method="POST" action="{{ Route('userstore') }}">
{!! csrf_field() !!}
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label class="col-md-4 control-label">الاسم</label>
<div class="col-md-6">
<input type="text" class="form-control" name="name" value="{{ old('name') }}">
@if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group {{$errors->has('UserName') ? 'has-error' : ''}}">
<label class="col-md-4 control-label">اسم المستخدم</label>
<div class="col-md-6">
<input type="text" class="form-control" name="UserName" value="{{ old('UserName') }}">
@if ($errors->has('UserName'))
<span class="help-block">
<strong>{{ $errors->first('UserName') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group {{$errors->has('Phone') ? 'has-error' : ''}}">
<label class="col-md-4 control-label">رقم الجوال</label>
<div class="col-md-6">
<input type="text" class="form-control" name="Phone" value="{{old('Phone')}}">
@if ($errors->has('Phone'))
<span class="help-block">
<strong>{{ $errors->first('Phone') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group {{$errors->has('SSN') ? 'has-error' : ''}}">
<label class="col-md-4 control-label">الرقم الوطني</label>
<div class="col-md-6">
<input type="text" class="form-control" name="SSN" value="{{old('SSN')}}">
@if ($errors->has('SSN'))
<span class="help-block">
<strong>{{ $errors->first('SSN') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group {{$errors->has('SDT') ? 'has-error' : ''}}">
<label class="col-md-4 control-label">نوع الوثيقة</label>
<div class="col-md-6">
<input type="text" class="form-control" name="SDT" value="{{old('SDT')}}">
@if ($errors->has('SDT'))
<span class="help-block">
<strong>{{ $errors->first('SDT') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group {{$errors->has('Country') ? 'has-error' : ''}}">
<label class="col-md-4 control-label">الدولة</label>
<div class="col-md-6">
<select class="form-control" name="Country">
<option>الاردن</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-4 control-label">المدينة</label>
<div class="col-md-6">
<select class="form-control" name="City" >
<option>عمان</option>
</select>
</div>
</div>
<div class="form-group {{$errors->has('Location') ? 'has-error' : ''}}">
<label class="col-md-4 control-label">اسم الشارع</label>
<div class="col-md-6">
<input type="text" class="form-control" name="Location" value="{{old('Location')}}">
@if ($errors->has('Location'))
<span class="help-block">
<strong>{{ $errors->first('Location') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }} ltr-input">
<label class="col-md-4 control-label">البريد الإلكتروني</label>
<div class="col-md-6">
<input type="email" class="form-control" name="email" value="{{ old('email') }}">
@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }} ltr-input">
<label class="col-md-4 control-label">كلمة المرور</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password">
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('password_confirmation') ? ' has-error' : '' }} ltr-input">
<label class="col-md-4 control-label">تأكيد كلمة المرور</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password_confirmation">
@if ($errors->has('password_confirmation'))
<span class="help-block">
<strong>{{ $errors->first('password_confirmation') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-primary pull-right">
<i class="fa fa-btn fa-user"></i> تسجيل
</button>
</div>
</div>
</form>
顺便说一句,这是 laravel 5.1
我通过将我的项目从 laravel 5.1 升级到 5.5
来解决问题