欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

laravel unique驗(yàn)證、確認(rèn)密碼confirmed驗(yàn)證以及密碼修改驗(yàn)證的方法

 更新時(shí)間:2019年10月16日 10:40:46   作者:tang05709  
這篇文章主要介紹了laravel unique驗(yàn)證、確認(rèn)密碼confirmed驗(yàn)證以及密碼修改驗(yàn)證的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

confirmed

驗(yàn)證字段必須有一個(gè)匹配字段 foo_confirmation,例如,如果驗(yàn)證字段是 password,必須輸入一個(gè)與之匹配的 password_confirmation 字段。

same:field

給定字段和驗(yàn)證字段必須匹配

 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)驗(yàn)證,上面的驗(yàn)證方式在update的時(shí)候會(huì)出問題,修改的時(shí)候會(huì)驗(yàn)證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句話是: 

有時(shí),您可能希望在唯一檢查期間忽略給定的ID。

當(dāng)然,您需要驗(yàn)證電子郵件地址是否唯一。但是,如果用戶僅更改名稱字段而不更改電子郵件字段,則不希望拋出驗(yàn)證錯(cuò)誤,因?yàn)橛脩粢呀?jīng)是電子郵件地址的所有者,為了指示驗(yàn)證者忽略用戶的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),
    ],

在更新密碼時(shí),我們需要驗(yàn)證舊的密碼是否正確,那我們需要使用自定義驗(yàn)證。

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àn)證失敗時(shí)應(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.');
      }
    },
  ],
]);

所以密碼是否正確可以這樣驗(yàn)證

'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);
 }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • session 加入redis的實(shí)現(xiàn)代碼

    session 加入redis的實(shí)現(xiàn)代碼

    本篇文章主要介紹了session 加入redis 的實(shí)例,對(duì)session 進(jìn)行了詳細(xì)介紹,并提供了代碼實(shí)例,需要的朋友可以參考下
    2016-07-07
  • Thinkphp5框架ajax接口實(shí)現(xiàn)方法分析

    Thinkphp5框架ajax接口實(shí)現(xiàn)方法分析

    這篇文章主要介紹了Thinkphp5框架ajax接口實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了thinkPHP5 ajax交互相關(guān)操作技巧,需要的朋友可以參考下
    2019-08-08
  • Yii2主題(Theme)用法詳解

    Yii2主題(Theme)用法詳解

    這篇文章主要介紹了Yii2主題(Theme)用法,結(jié)合實(shí)例形式分析了Yii2主題(Theme)的配置方式、函數(shù)與相關(guān)屬性的使用技巧,需要的朋友可以參考下
    2016-07-07
  • thinkPHP框架中執(zhí)行原生SQL語句的方法

    thinkPHP框架中執(zhí)行原生SQL語句的方法

    這篇文章主要介紹了thinkPHP框架中執(zhí)行原生SQL語句的方法,結(jié)合實(shí)例形式分析了thinkPHP中執(zhí)行原生SQL語句的相關(guān)操作技巧,并簡(jiǎn)單分析了query與execute方法的使用區(qū)別,需要的朋友可以參考下
    2017-10-10
  • PHP解決高并發(fā)問題(opcache)

    PHP解決高并發(fā)問題(opcache)

    這篇文章主要介紹了PHP解決高并發(fā)問題(opcache),本篇文章通過簡(jiǎn)要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-07-07
  • Smarty中調(diào)用FCKeditor的方法

    Smarty中調(diào)用FCKeditor的方法

    這篇文章主要介紹了Smarty中調(diào)用FCKeditor的方法,對(duì)比常見的錯(cuò)誤方法講述了Smarty中調(diào)用FCKeditor的實(shí)現(xiàn)過程,是非常實(shí)用的技巧,需要的朋友可以參考下
    2014-10-10
  • Yii實(shí)現(xiàn)單用戶博客系統(tǒng)文章詳情頁插入評(píng)論表單的方法

    Yii實(shí)現(xiàn)單用戶博客系統(tǒng)文章詳情頁插入評(píng)論表單的方法

    這篇文章主要介紹了Yii實(shí)現(xiàn)單用戶博客系統(tǒng)文章詳情頁插入評(píng)論表單的方法,結(jié)合實(shí)例分析了Yii實(shí)現(xiàn)文章詳情頁評(píng)論表單功能的具體技巧,需要的朋友可以參考下
    2015-12-12
  • php遞歸函數(shù)三種實(shí)現(xiàn)方法及如何實(shí)現(xiàn)數(shù)字累加

    php遞歸函數(shù)三種實(shí)現(xiàn)方法及如何實(shí)現(xiàn)數(shù)字累加

    實(shí)現(xiàn)遞歸函數(shù)有哪些方法呢?如何用遞歸函數(shù)實(shí)現(xiàn)數(shù)字累加?這篇文章就主要介紹php遞歸函數(shù)三種實(shí)現(xiàn)方法及如何實(shí)現(xiàn)數(shù)字累加,需要的朋友可以參考下。
    2015-08-08
  • php模板原理講解

    php模板原理講解

    php各種MVC框架采用頁面和代碼分離,通過模板將變量賦值到頁面,以及模板引擎,那么php模板賦值的原理是什么呢
    2013-11-11
  • 基于swoole實(shí)現(xiàn)多人聊天室

    基于swoole實(shí)現(xiàn)多人聊天室

    這篇文章主要為大家詳細(xì)介紹了基于swoole實(shí)現(xiàn)多人聊天室,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06

最新評(píng)論