从自定义验证对象中检索参数
Retrieve parameters from custom validation object
我有基本习惯validation rule
。在
public function passes($attribute, $value)
{
foreach ($parameters as $key)
{
if ( ! empty(Input::get($key)) )
{
return false;
}
}
return true;
}
我已经定义了我的规则。我,虽然需要从 rule
中检索 parameters
,但是 passes
方法没有将其作为 argument
.
提供
如果我使用提供 4 arguments: $attribute, $value, $parameters, $validator
的样式 Validator:extends...
。然后我可以轻松使用parameters
,不幸的是我必须这样使用。
编辑:
清除问题。我想检索 parameters of the rule
,就像用其他编码方式一样:
'not_empty:user_id'
。冒号后面的值数组。
我认为唯一的方法是在使用规则对象时从请求中检索它。
例如:
public function passes($attribute, $value)
{
foreach ($parameters as $key) {
// Or using \Request::input($key) if you want to use the facade
if (!empty(request()->input($key)) {
return false;
}
}
return true;
}
编辑:---
自定义规则对象只是一个对象。如果你想给它传递更多的参数,你可以在它的构造函数中:
$request->validate([
'name' => ['required', new MyCustomRule('param', true, $foo)],
]);
然后保存它们并在您的 passes
函数中使用它们。
public function __construct($myCustomParam){
$this->myCustomParam = $myCustomParam;
}
然后在你的通行证函数中使用:
$this->myCustomParam
我有基本习惯validation rule
。在
public function passes($attribute, $value)
{
foreach ($parameters as $key)
{
if ( ! empty(Input::get($key)) )
{
return false;
}
}
return true;
}
我已经定义了我的规则。我,虽然需要从 rule
中检索 parameters
,但是 passes
方法没有将其作为 argument
.
如果我使用提供 4 arguments: $attribute, $value, $parameters, $validator
的样式 Validator:extends...
。然后我可以轻松使用parameters
,不幸的是我必须这样使用。
编辑:
清除问题。我想检索 parameters of the rule
,就像用其他编码方式一样:
'not_empty:user_id'
。冒号后面的值数组。
我认为唯一的方法是在使用规则对象时从请求中检索它。
例如:
public function passes($attribute, $value)
{
foreach ($parameters as $key) {
// Or using \Request::input($key) if you want to use the facade
if (!empty(request()->input($key)) {
return false;
}
}
return true;
}
编辑:---
自定义规则对象只是一个对象。如果你想给它传递更多的参数,你可以在它的构造函数中:
$request->validate([
'name' => ['required', new MyCustomRule('param', true, $foo)],
]);
然后保存它们并在您的 passes
函数中使用它们。
public function __construct($myCustomParam){
$this->myCustomParam = $myCustomParam;
}
然后在你的通行证函数中使用:
$this->myCustomParam