服务器之家

服务器之家 > 正文

laravel unique验证、确认密码confirmed验证以及密码修改验证的方法

时间:2021-09-06 17:04     来源/作者:tang05709

confirmed

验证字段必须有一个匹配字段 foo_confirmation,例如,如果验证字段是 password,必须输入一个与之匹配的 password_confirmation 字段。

same:field

给定字段和验证字段必须匹配

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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);
}

经验证,上面的验证方式在update的时候会出问题,修改的时候会验证unique,导致不能保存,所以需要修改下。

官网说:

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。

当然,您需要验证电子邮件地址是否唯一。但是,如果用户仅更改名称字段而不更改电子邮件字段,则不希望抛出验证错误,因为用户已经是电子邮件地址的所有者,为了指示验证者忽略用户的ID,我们将使用Rule该类来流畅地定义规则。

?
1
2
3
4
5
6
7
8
use Illuminate\Validation\Rule;
 
Validator::make($data, [
  'email' => [
    'required',
    Rule::unique('users')->ignore($user->id),
  ],
]);

所以修改为

?
1
2
3
4
'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在验证失败时应调用的回调。

?
1
2
3
4
5
6
7
8
9
10
11
$validator = Validator::make($request->all(), [
  'title' => [
    'required',
    'max:255',
    function($attribute, $value, $fail) {
      if ($value === 'foo') {
        return $fail($attribute.' is invalid.');
      }
    },
  ],
]);

所以密码是否正确可以这样验证

?
1
2
3
4
5
6
7
8
9
10
'old_password' => [
     'required',
     function($attribute, $value, $fail) use ($manager)
     {
      if (!Hash::check($value, $manager->password))
      {
       return $fail(__('tyvalidation.old_password'));
      }
     },
    ],

所有代码如下:

create.html

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<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

?
1
2
3
4
5
6
7
8
9
10
$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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
$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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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);
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/tang05709/article/details/87098819

相关文章

热门资讯

yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021年耽改剧名单 2021要播出的59部耽改剧列表
2021年耽改剧名单 2021要播出的59部耽改剧列表 2021-03-05
返回顶部