在我的laravel项目中,我使用js验证器,但在表单更新过程中,Validation不起作用。如果我没有做任何更改,我的表格将不会提交。有时它会在电子邮件字段中显示错误。
以下是我的验证码,
protected $edituserValidationRules = [
'user_name' => 'required',
'email' => 'required|email|max:255|unique:users',
];
以下是通过验证的代码和查看的其他内容
public function viewUsers($id)
{
$users = User::find($id);
$validator = JsValidator::make($this->edituserValidationRules,[],[],'#useredit');
return view('user.update')->with(['validator' => $validator,'users'=> $users]);
}
以下是更新特定用户的代码
public function updateUsers(Request $request , $id)
{
$this->validate($request, [
'email' => 'required|email|max:255|unique:users' . $request->id
]);
$postdata = $request->all();
$user = new User;
$user = User::find($id);
$hashedRandomPassword = Hash::make($postdata['password'], [
'rounds' => 12
]);
$api_key = Str::random(16);
$checkApiKey = User::where('api_key', $api_key)->first();
if(!empty($checkApiKey)) {
$api_key = Str::random(16);
}
$user = new User;
$user->name=$postdata['user_name'];
$user->email=$postdata['email'];
$user->password= $hashedRandomPassword;
$user->ip_address=$request->ip();
$user->api_key=$api_key;
if($user->save()){
Session::flash('message', 'User Updated Sucessfully');
Session::flash('msgclass', 'alert-success');
}
return redirect('users');
}
以下是查看页面中的代码
@extends('user.layout.app')
@section('content')
<script src="{{ url('js/user/location.js') }}"></script>
<div class="container-fluid add-location">
<div class="row">
<div class="col-md-12">
<div class="card">
<form method="post" action="{!! route('updateUsers', [$users->id]); !!}" name="useredit" id="useredit" enctype="multipart/form-data" novalidate>
{{ csrf_field() }}
<div class="card-header">
<h4 class="card-title"> Edit User </h4>
</div>
@if(!empty($errors->all()))
<div class="row"> @foreach ($errors->all() as $error)
<div class="col-lg-12">
<div class="alert alert-danger"> <span>{{ $error }}</span> </div>
</div>
@endforeach </div>
@endif
<div class="card-content">
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<label class="control-label">Full Name
<star>*</star>
</label>
<input id="user_name" name="user_name" class="controls form-control" type="text" placeholder="User Name" value="{{$users->name}}">
@if ($errors->has('user_name')) <span class="help-block"> {{ $errors->first('user_name') }} </span> @endif </div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
<label for="email" class="control-label">Email
<star>*</star>
</label>
<input id="email" type="email" class="form-control" name="email" value="{{$users->email}}" placeholder="Email">
@if ($errors->has('email'))
<span class="help-block">
<strong>{{ $errors->first('email') }}</strong>
</span>
@endif
</div>
</div>
<!-- <div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="password" class="control-label">Password
<star>*</star>
</label>
<input id="password" type="password" class="form-control" name="password" value="{{bcrypt($users->password)}}">
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div> -->
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-6">
<div class="col-xs-12 col-sm-12 col-md-12 form-action">
<button type="submit" class="btn btn-fill btn-info">Submit</button>
<a href="" class="btn btn-default btn-fill">Cancel</a> </div>
</div>
</form>
</div>
</div>
</div>
</div>
{!! $validator !!}
@endsection
这里的问题是什么,在添加客户的过程中,我正在检查电子邮件是否已经存在,同样也在检查这里。
我不知道发生了什么?请帮助
尝试更改此行
$this->validate($request, [
'email' => 'required|email|max:255|unique:users' . $request->id
]);
至
$request->validate([
'email' => 'required|email|max:255|unique:users,email,'.$id
]);
添加',id'语句末尾,用于阻止对当前id的电子邮件存在检查…
$request->validate([
'email' => 'required|email|max:255|unique:users,email,'.$request->id.',id',
]);