0

我要做的是检查从表格发送的日期的年份是否具有正确的值。正确的值是在验证器的构造函数中作为参数给出的计划年份。

我使用下一个自定义验证器从标题中收到消息。这是因为变量 $this->$plan_year,但我不明白为什么。这是验证器:

class MyValidate_YearValidator extends Zend_Validate_Abstract
{
    const MSG_YEAR = '';

    private $plan_year = 0;

    public $minimum = 0;
    public $maximum = 0;

    protected $_messageVariables = array(
        'min' => 'minimum',
        'max' => 'maximum'
    );

    protected $_messageTemplates = array(
        self::MSG_YEAR => "Valoarea '%value%' nu este corecta! Anul trebuie sa aibe una din urmatoarele doua valori: '%min%' sau '%max%'."
    );

    public function __construct( $plan_year )
    {
        $this->$plan_year = $plan_year;

    }

    public function isValid($value)
    {

        $this->_setValue($value);

        $anul = substr($value, 0, 4);

        //here is the problem
        $this->minimum = $this->$plan_year;
        $this->maximum = $this->$plan_year + 1;

        if ($anul <> $this->minimum && $anul <> $this->maximum && $value != '') {
            $this->_error(self::MSG_YEAR);
            return false;
        }

        return true;
    }
}

谢谢!索林

4

1 回答 1

3

引用类成员时没有美元符号(仅当它们是静态的时)!否则,该名称将被视为另一个变量。

$this->$plan_year // wrong
$this->plan_year  // correct
于 2012-05-07T14:06:30.553 回答