我正在使用 Laravel,它是验证器。
我的控制器中有以下代码:
class ResellerController extends BaseController{
public function add() {
//some code before
$userValidator = new App\Services\Validators\UserCreateValidator();
//HERE I WANT TO REMOVE THE company KEY FROM THE RULES IN THE USERS CREATE VALIDATOR
$userValidator->removeRule('company');
//code execution continues
}
}
UserCreateValidator 扩展了一个父 Validator 类:
namespace App\Services\Validators;
class UserCreateValidator extends Validator {
public static $rules = array(
'firstName' => 'required',
'lastName' => 'required',
'email' => 'required|email|unique:users',
'company' => 'required'
);
}
这是基本的 Validator 类:
namespace App\Services\Validators;
abstract class Validator {
/**
* Validation rules
* @var array
*/
public static $rules;
//THIS CODE DOES NOT WORK IN THE CONTROLLER UP
public static function removeRule($ruleKey){
if(is_array($ruleKey))
{
foreach($ruleKey as $key)
{
if(!array_key_exists($key, static::$rules)) continue;
unset(static::$rules[$key]);
}
return true;
}
if(!array_key_exists($ruleKey, static::$rules)) //return false;
unset(static::$rules['company']);
return true;
}
}
static::$rules[$key]
in的取消设置ResellerController
不起作用。
我可以在XDEBUG会话中(在执行此行之后)看到as 属性static::$rules['company']
中仍然存在。UserCreateValidator
我认为后期静态绑定应该解决这个问题?怎么了?