LoginSignup
0
0

More than 5 years have passed since last update.

LaravelのValidatorでカスタムルールをつくる

Posted at

例えばフィールドの値よりも、入力値が大きい場合はエラーをだすなど、
validatorでextendを使うとルールを拡張できる

$valueは入力値。returnでfalseになるとエラーとして返される。

Validator::extend('pointcheck', function($attribute, $value, $parameters, $validator){
 $user = Auth::user();
 return TotalPoint::where('user_id',$user->id)->where('give_point','>=',$value)->exists();
});

$rules = [
 'point' => 'required|integer|min:1|pointcheck',
];

$messages = [
 'point.pointcheck' => self::$messages['point.pointcheck'],
];
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0