为每个字段设置海关Zend表单验证消息

时间:2014-04-29 13:55:18

标签: php zend-framework zend-form zend-validate

我想为表单中的字段创建自定义错误消息。 在这些字段中,您需要输入一个必须介于1 - 999999999999999999999.99之间的数字范围,如果它低于1或超过99999等。我想要显示某个消息。

这是我目前的代码,但是当我尝试打开表单时,我得到的是一个空白的白色屏幕。

$fee = new Zend_Form_Element_Text('job_salary');
$fee->setLabel("Salary ");
$fee->setAttrib('class', 'input-block-level');
$fee->addDecorator("ViewHelper");
$fee->addValidator('Float');
//$fee->addFilter("Alnum");
$fee->addValidator(new Zend_Validate_JobValidator(), true);
$fee = $salary->getMessageTemplates();
$fee->setMessage(
        Zend_Validate_JobValidator::NOT_BETWEEN_SALARY
        );
$fee->setRequired(true);
$this->addElement($fee);

这是其中一个字段的功能。

然后我创建了一个单独的验证器类而不是使用Zend类。

const MSG_NUMERIC = 'msgNumeric';
const NOT_BETWEEN = 'notBetween';
const NOT_BETWEEN_SALARY = 'notBetween';
const NOT_BETWEEN_STRICT = 'notBetweenStrict';

public $minimum = 1;
public $maximum = 99999999999999999999999999999.99;
protected $_messageVariables = array(
    'min' => 'minimum',
    'max' => 'maximum'
);
protected $_messageTemplates = array(
    self::MSG_NUMERIC => "'%value%' is not numeric",
    self::NOT_BETWEEN => "'%value%' is not between '%min%' and '%max%' inclusively  - Please fill in the correct amount for fees",
    self::NOT_BETWEEN_SALARY => "'%value'is not between '%min%' and '%max%', inclusively - Please fill in the correct amount for this position ",
    self::NOT_BETWEEN_STRICT => "'%value%' is not strictly between '%min%' and '%max%' inclusively  - Please fill in the fee for this placement"
);

public function isValid($value) {
    $this->_setValue($value);

    if (!is_numeric($value)) {
        $this->_error(self::MSG_NUMERIC);
        return false;
    }

    if ($value < $this->minimum) {
        $this->_error(self::NOT_BETWEEN);
        return false;
    }

    if ($value < $this->minimum) {
        $this->_error(self::NOT_BETWEEN_SALARY);
        return false;
    }

    if ($value > $this->maximum) {
        $this->_error(self::NOT_BETWEEN_STRICT);
        return false;
    }

    return true;
}

}

0 个答案:

没有答案