您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“l(fā)aravel中如何實現(xiàn)unique驗證、確認(rèn)密碼confirmed驗證以及密碼修改驗證”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“l(fā)aravel中如何實現(xiàn)unique驗證、確認(rèn)密碼confirmed驗證以及密碼修改驗證”這篇文章吧。
confirmed
驗證字段必須有一個匹配字段 foo_confirmation,例如,如果驗證字段是 password,必須輸入一個與之匹配的 password_confirmation 字段。
same:field
給定字段和驗證字段必須匹配
protected $fillable = ['name', 'password']; public static $rules = [ 'name' => 'required|unique:managers', 'password' => 'required|confirmed', 'password_confirmation' => 'required|same:password' ]; public static function error_message() { return [ 'name.required' => __('tyvalidation.name'), 'name.unique' => __('tyvalidation.unique'), 'password.required' => __('tyvalidation.password'), 'password.confirmed' => __('tyvalidation.confirmed'), ]; } public function setPasswordAttribute($value) { $this->attributes['password'] = Hash::make($value); }
經(jīng)驗證,上面的驗證方式在update的時候會出問題,修改的時候會驗證unique,導(dǎo)致不能保存,所以需要修改下。
官網(wǎng)說:
Sometimes, you may wish to ignore a given ID during the unique check. For example, consider an "update profile" screen that includes the user's name, e-mail address, and location. Of course, you will want to verify that the e-mail address is unique. However, if the user only changes the name field and not the e-mail field, you do not want a validation error to be thrown because the user is already the owner of the e-mail address.
To instruct the validator to ignore the user's ID, we'll use the Rule class to fluently define the rule. In this example, we'll also specify the validation rules as an array instead of using the |character to delimit the rules:
重要的2句話是:
有時,您可能希望在唯一檢查期間忽略給定的ID。
當(dāng)然,您需要驗證電子郵件地址是否唯一。但是,如果用戶僅更改名稱字段而不更改電子郵件字段,則不希望拋出驗證錯誤,因為用戶已經(jīng)是電子郵件地址的所有者,為了指示驗證者忽略用戶的ID,我們將使用Rule該類來流暢地定義規(guī)則。
use Illuminate\Validation\Rule; Validator::make($data, [ 'email' => [ 'required', Rule::unique('users')->ignore($user->id), ], ]);
所以修改為
'name' => [ 'required', Rule::unique('managers')->ignore($id), ],
在更新密碼時,我們需要驗證舊的密碼是否正確,那我們需要使用自定義驗證。
Using Closures
If you only need the functionality of a custom rule once throughout your application, you may use a Closure instead of a rule object. The Closure receives the attribute's name, the attribute's value, and a $fail callback that should be called if validation fails:
Closure接收屬性的名稱,屬性的值以及$fail
在驗證失敗時應(yīng)調(diào)用的回調(diào)。
$validator = Validator::make($request->all(), [ 'title' => [ 'required', 'max:255', function($attribute, $value, $fail) { if ($value === 'foo') { return $fail($attribute.' is invalid.'); } }, ], ]);
所以密碼是否正確可以這樣驗證
'old_password' => [ 'required', function($attribute, $value, $fail) use ($manager) { if (!Hash::check($value, $manager->password)) { return $fail(__('tyvalidation.old_password')); } }, ],
所有代碼如下:
create.html
<div class="form-group"> <label>{!! __('tycms.name') !!}</label> <div class="input-group"> <div class="input-group-prepend"> <span class="input-group-text change-bg">T</span> </div> <input type="text" class="form-control is-invalid" name="name" value="" placeholder="{!! __('tycms.name') !!}" required /> @foreach ($errors->get('name') as $message) <div class="invalid-feedback"> {{ $message }} </div> @endforeach </div> </div> <div class="form-group"> <label>{!! __('tycms.password') !!}</label> <div class="input-group"> <div class="input-group-prepend"> <span class="input-group-text change-bg">T</span> </div> <input type="password" class="form-control is-invalid" name="password" value="" placeholder="{!! __('tycms.password') !!}" required /> @foreach ($errors->get('password') as $message) <div class="invalid-feedback"> {{ $message }} </div> @endforeach </div> </div> <div class="form-group"> <label>{!! __('tycms.confirm_password') !!}</label> <div class="input-group"> <div class="input-group-prepend"> <span class="input-group-text change-bg">T</span> </div> <input type="password" class="form-control is-invalid" name="password_confirmation" value="" placeholder="{!! __('tycms.confirm_password') !!}" required /> @foreach ($errors->get('password') as $message) <div class="invalid-feedback"> {{ $message }} </div> @endforeach </div> </div>
store
$input_all = $request->all(); $validator = Validator::make($input_all, Manager::rules(), Manager::error_message()); if ($validator->fails()) { return redirect() ->action($this->class_basename . '@create') ->withErrors($validator) ->withInput(); } $model = Manager::create($input_all);
edit.html
<div class="form-group"> <label>{!! __('tycms.name') !!}</label> <div class="input-group"> <div class="input-group-prepend"> <span class="input-group-text change-bg">T</span> </div> <input type="text" class="form-control is-invalid" name="name" value="{{ $model->name }}" readonly="readonly" placeholder="{!! __('tycms.name') !!}" required /> @foreach ($errors->get('name') as $message) <div class="invalid-feedback"> {{ $message }} </div> @endforeach </div> </div> <div class="form-group"> <label>{!! __('tycms.old_password') !!}</label> <div class="input-group"> <div class="input-group-prepend"> <span class="input-group-text change-bg">T</span> </div> <input type="password" class="form-control is-invalid" name="old_password" value="" placeholder="{!! __('tycms.old_password') !!}" required /> @foreach ($errors->get('old_password') as $message) <div class="invalid-feedback"> {{ $message }} </div> @endforeach </div> </div> <div class="form-group"> <label>{!! __('tycms.password') !!}</label> <div class="input-group"> <div class="input-group-prepend"> <span class="input-group-text change-bg">T</span> </div> <input type="password" class="form-control is-invalid" name="password" value="" placeholder="{!! __('tycms.password') !!}" required /> @foreach ($errors->get('password') as $message) <div class="invalid-feedback"> {{ $message }} </div> @endforeach </div> </div> <div class="form-group"> <label>{!! __('tycms.confirm_password') !!}</label> <div class="input-group"> <div class="input-group-prepend"> <span class="input-group-text change-bg">T</span> </div> <input type="password" class="form-control is-invalid" name="password_confirmation" value="" placeholder="{!! __('tycms.confirm_password') !!}" required /> @foreach ($errors->get('password') as $message) <div class="invalid-feedback"> {{ $message }} </div> @endforeach </div> </div>
update
$input_all = $request->all(); $model = $this->findById($id); $validator = Validator::make($input_all, Manager::rules($id, $model), Manager::error_message()); if ($validator->fails()) { return redirect() ->action($this->class_basename . '@edit', ['id' => $id]) ->withErrors($validator) ->withInput(); } $model->fill($input_all); $model->save();
Models\Manager
protected $table = 'managers'; protected $fillable = ['name', 'password']; /*public static $rules = [ 'name' => 'required|unique:managers', 'password' => 'required|confirmed', 'password_confirmation' => 'required|same:password' ];*/ public static function rules ($id = null, $manager = null) { if (empty($id)) { $rules = [ 'name' => 'required|unique:managers', 'password' => 'required|confirmed', 'password_confirmation' => 'required|same:password' ]; } else { $rules = [ 'name' => [ 'required', Rule::unique('managers')->ignore($id), ], 'old_password' => [ 'required', function($attribute, $value, $fail) use ($manager) { if (!Hash::check($value, $manager->password)) { return $fail(__('tyvalidation.old_password')); } }, ], 'password' => 'required|confirmed', 'password_confirmation' => 'required|same:password' ]; } return $rules; } public static function error_message() { return [ 'name.required' => __('tyvalidation.name'), 'name.unique' => __('tyvalidation.unique'), 'password.required' => __('tyvalidation.password'), 'password.confirmed' => __('tyvalidation.confirmed'), ]; } public function setPasswordAttribute($value) { $this->attributes['password'] = Hash::make($value); }
以上是“l(fā)aravel中如何實現(xiàn)unique驗證、確認(rèn)密碼confirmed驗證以及密碼修改驗證”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。