Closure接收属性的名称,属性的值以及$fail
在验证失败时应调用的回调。
$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);
内容版权声明:除非注明,否则皆为本站原创文章。