我是Laravel的新手,我尝试验证请求。我必须遵循请求类:
namespace App\Http\Requests;
class TestRequest extends FormRequest
{
protected function rules()
{
return [
'group_id' => 'required|exists:groups,id,deleted_at,NULL|exists:group_users,group_id,user_id,' . \Auth::user()->id
];
}
}
我的问题是:
我的问题是:
PS:我使用的是Laravel 5.3
答案 0 :(得分:1)
我建议编写自定义规则。请查看以下链接,了解将其添加到代码中的位置
https://laravel.com/docs/5.3/validation#custom-validation-rules
Validator::extend('group_check', function($attribute, $value, $parameters, $validator) {
// Do custom exists check 1;
$group = Group::where('id', $value)->where('deleted_at', 'null')->first();
if (!$group) {
return false;
}
// Do custom exists check 2;
});
Validator::replacer('group_check', function($message, $value, $parameters, $validator) {
// Do custom exists check 1 but instead of returning false, return a custom message
// Do custom exists check 2 return a custom message
});