如何在Laravel的验证规则中使用两个属性。这是我的函数,其中有2个变量groupid
和grouppassword
。目前仅使用groupid
。
public function groupPasswordValidationReal(Request $request){
$groupid = $request->input('groupid');
$grouppassword = $request->input('grouppassword');
$validator = \Validator::make($request->all(), [
'groupid' => [new ValidRequest] //How to pass grouppassword in this function???
]);
return redirect('home')->with('success', 'Request is send!');
}
如何将两者都传递给我的验证类?在这里,您可以看到验证类中的功能。
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use App\Group;
class ValidTest implements Rule
{
public function passes($attribute, $value)
{
//$value is groupid
$validPassword = Group::where([['idgroups', $value],['group_password', /*Here I need grouppassword*/]])->first();
if($validPassword){
return true;
}else{
return false;
}
}
public function message()
{
return 'Wrong Password!';
}
}
答案 0 :(得分:1)
将属性和构造函数添加到您的 ValidTest类。将所需的值作为参数传递给新对象。
ValidTest.php
namespace App\Rules;
use Illuminate\Contracts\Validation\Rule;
use App\Group;
class ValidTest implements Rule
{
/**
* The group password.
*
* @var string
*/
public $groupPassword;
/**
* Create a new rule instance.
*
* @param \App\Source $source
* @param string $branch
* @return void
*/
public function __construct($groupPassword)
{
$this->groupPassword = $groupPassword;
}
public function passes($attribute, $value)
{
//$value is groupid
//$this->groupPassword is group_password
$validPassword = Group::where([['idgroups', $value],['group_password', $this->groupPassword]])->first();
if($validPassword){
return true;
}else{
return false;
}
}
public function message()
{
return 'Wrong Password!';
}
}
控制器
public function groupPasswordValidationReal(Request $request){
$groupid = $request->input('groupid');
$grouppassword = $request->input('grouppassword');
$validator = \Validator::make($request->all(), [
'groupid' => new ValidTest($grouppassword)
]);
return redirect('home')->with('success', 'Request is send!');
}
来源:custom-validation-rules-in-laravel-5-5
注意:这不是经过测试的解决方案。
答案 1 :(得分:0)
我认为您可以在此处使用闭包。像这样:
$validator = \Validator::make($request->all(), function() use ($groupid, $grouppassword) {
$validPassword = Group::where([['idgroups', $value],['group_password', $grouppassword]])->first();
if($validPassword){
return true;
}else{
return false;
}
}
我还没有运行它。
答案 2 :(得分:0)
像使用Rule constructor
class ValidUser implements Rule
{
private $grouppassword;
/**
* Create a new rule instance.
*
* @return void
*/
public function __construct($grouppassword)
{
$this->grouppassword = $grouppassword;
}
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
// $this->grouppassword
// $groupid = $value
// Do your logic...
return true;
}
/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The :attribute must something...';
}
}
用法
Route::post('/user', function (Request $request) {
// Parameter validation
$validator = Validator::make($request->all(), [
'groupid' => 'required',
'grouppassword' => ['required', new ValidUser($request->groupid), ]
]);
if ($validator->fails())
return response()->json(['error' => $validator->errors()], 422);
});
答案 3 :(得分:0)
如果您从Request类调用验证,则可以访问额外的值,例如
request()->validate([
'groupid' => [new ValidTest(request()->input('grouppassword'))]
]);