php 判断同一字段是否全相同,php – Zend_Form:如何检查2个字段是否相同

这段代码定义了一个名为`Zend_Validate_IdenticalField`的验证器类,该类继承自`Zend_Validate_Abstract`。类的主要目的是验证输入值是否与上下文数组中指定字段的值相匹配。如果字段名未提供、字段不存在于上下文或值不匹配,验证器将返回错误消息。类包含用于设置和获取字段名及字段标题的方法,并提供了错误消息模板和变量。
摘要由CSDN通过智能技术生成

当我寻找相同的东西时,我发现这个非常好用于相同字段的通用验证器.我现在没有找到它所以我只是发布代码…

class Zend_Validate_IdenticalField extends Zend_Validate_Abstract {

const NOT_MATCH = 'notMatch';

const MISSING_FIELD_NAME = 'missingFieldName';

const INVALID_FIELD_NAME = 'invalidFieldName';

/**

* @var array

*/

protected $_messageTemplates = array(

self::MISSING_FIELD_NAME =>

'DEVELOPMENT ERROR: Field name to match against was not provided.',

self::INVALID_FIELD_NAME =>

'DEVELOPMENT ERROR: The field "%fieldName%" was not provided to match against.',

self::NOT_MATCH =>

'Does not match %fieldTitle%.'

);

/**

* @var array

*/

protected $_messageVariables = array(

'fieldName' => '_fieldName',

'fieldTitle' => '_fieldTitle'

);

/**

* Name of the field as it appear in the $context array.

*

* @var string

*/

protected $_fieldName;

/**

* Title of the field to display in an error message.

*

* If evaluates to false then will be set to $this->_fieldName.

*

* @var string

*/

protected $_fieldTitle;

/**

* Sets validator options

*

* @param string $fieldName

* @param string $fieldTitle

* @return void

*/

public function __construct($fieldName, $fieldTitle = null) {

$this->setFieldName($fieldName);

$this->setFieldTitle($fieldTitle);

}

/**

* Returns the field name.

*

* @return string

*/

public function getFieldName() {

return $this->_fieldName;

}

/**

* Sets the field name.

*

* @param string $fieldName

* @return Zend_Validate_IdenticalField Provides a fluent interface

*/

public function setFieldName($fieldName) {

$this->_fieldName = $fieldName;

return $this;

}

/**

* Returns the field title.

*

* @return integer

*/

public function getFieldTitle() {

return $this->_fieldTitle;

}

/**

* Sets the field title.

*

* @param string:null $fieldTitle

* @return Zend_Validate_IdenticalField Provides a fluent interface

*/

public function setFieldTitle($fieldTitle = null) {

$this->_fieldTitle = $fieldTitle ? $fieldTitle : $this->_fieldName;

return $this;

}

/**

* Defined by Zend_Validate_Interface

*

* Returns true if and only if a field name has been set, the field name is available in the

* context, and the value of that field name matches the provided value.

*

* @param string $value

*

* @return boolean

*/

public function isValid($value, $context = null) {

$this->_setValue($value);

$field = $this->getFieldName();

if (empty($field)) {

$this->_error(self::MISSING_FIELD_NAME);

return false;

} elseif (!isset($context[$field])) {

$this->_error(self::INVALID_FIELD_NAME);

return false;

} elseif (is_array($context)) {

if ($value == $context[$field]) {

return true;

}

} elseif (is_string($context) && ($value == $context)) {

return true;

}

$this->_error(self::NOT_MATCH);

return false;

}

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值