标题: Zend Form 中如何验证2个password是否相同?
zhex
新手上路
Rank: 1



UID 33
精华 0
积分 0
帖子 3
翻译 0
原创 0
阅读权限 10
注册 2007-5-20
状态 离线
发表于 2008-7-16 13:02  资料  短消息  加为好友 
Zend Form 中如何验证2个password是否相同?

Zend Form 中如何验证2个password是否相同?
网上找了好长时间都没有找到答案, 请这里的高手帮帮忙

顶部
jasonqi
PHPEye Developer
Rank: 8Rank: 8



UID 275
精华 0
积分 0
帖子 74
翻译 0
原创 0
阅读权限 1
注册 2007-8-11
状态 离线
发表于 2008-7-16 13:44  资料  短消息  加为好友 
下面是我抄别人的,估计你还需要判断元素是否在表中存在等校验,照猫画虎就可以了

PHP代码如下:

<?php  
require_once 'Zend/Validate/Abstract.php';  
  
class 
My_Validate_Match extends Zend_Validate_Abstract 
{  
    const 
NOT_MATCH 'NotMatch';  
  
    protected 
$_messageTemplates = array(  
        
self::NOT_MATCH => "'%fieldName%' does not match"
    
);  
  
    
/** 
     * The fields that the current element needs to match 
     * 
     * @var array 
     */  
    
protected $_fieldsToMatch = array();  
  
    
/** 
     * Constructor of this validator 
     * 
     * The argument to this constructor is the third argument to the elements' addValidator 
     * method. 
     * 
     * @param array|string $fieldsToMatch 
     */  
    
public function __construct($fieldsToMatch = array()) {  
        if (
is_array($fieldsToMatch)) {  
            foreach (
$fieldsToMatch as $field) {  
                
$this->_fieldsToMatch[] = (string) $field;  
            }  
        } else {  
            
$this->_fieldsToMatch[] = (string) $fieldsToMatch;  
        }  
    }  
  
    
/** 
     * Check if the element using this validator is valid 
     * 
     * This method will compare the $value of the element to the other elements 
     * it needs to match. If they all match, the method returns true. 
     * 
     * @param $value string 
     * @param $context array All other elements from the form 
     * @return boolean Returns true if the element is valid 
     */  
    
public function isValid($value$context null) {  
        
$value = (string) $value;  
        
$this->_setValue($value);  
  
        
$error false;  
  
        foreach (
$this->_fieldsToMatch as $fieldName) {  
            if (!isset(
$context[$fieldName]) || $value !== $context[$fieldName]) {  
                
$error true;  
                
$errorMessage $fieldName ' does not match';
                
$this->setMessage($errorMessageself::NOT_MATCH);
                
$this->_error(self::NOT_MATCH);  
                break;  
            }  
        }  
  
        return !
$error;  
    }  
}  




顶部
lony
新手上路
Rank: 1



UID 1450
精华 1
积分 10
帖子 20
翻译 0
原创 1
阅读权限 10
注册 2008-7-2
状态 离线
发表于 2008-10-8 18:28  资料  短消息  加为好友 
->addValidator


怎么写呢?

[ 本帖最后由 lony 于 2008-10-13 14:17 编辑 ]

顶部
 


PHPEye开源社区


当前时区 GMT+8, 现在时间是 2008-11-21 19:05

    Powered by Discuz! 5.5.0  © 2001-2007 Comsenz Inc.
Processed in 0.008461 second(s), 6 queries , Gzip enabled

清除 Cookies - 联系我们 - PHPEye开源社区 - Archiver