0

我正在使用 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

我认为后期静态绑定应该解决这个问题?怎么了?

4

1 回答 1

0

问题已经解决了。它在以下的评论部分:

if(!array_key_exists($ruleKey, static::$rules)) //return false;

取消注释后,取消设置工作正常return false

愚蠢的错误:)

于 2014-12-03T12:30:44.323 回答