Zend Framework验证和/或过滤问题

时间:2012-03-30 11:02:50

标签: php zend-framework zend-form

我在过滤和验证下面的表单时遇到问题。如果以国家格式(07777654321)输入两个手机号码,则表单验证失败,但如果以国际格式(447777654321)输入,则验证。我的理解是首先应用过滤器,因此在应用相同的过滤器时,mobilemobile_confirm应该使用同一组过滤器进行处理。

因此,如果我在两个字段中输入07777654321,则会在两个字段中过滤到447777654321,然后应进行验证。我哪里错了。

我使用的是ZF 1.11.3。

<?php

class Test_Form_Check extends Zend_Form
{

    public function init()
    {

        $mobile= new Zend_Form_Element_Text( 'mobile' );
        $mobile->addFilter(new Test_Filter_MobileFilter('44'))
                ->addValidator(new Test_Validate_Mobile())
                ->setRequired( true );

        $mobile1= new Zend_Form_Element_Text( 'mobile_confirm' );
        $mobile1->addFilter(new Test_Filter_MobileFilter('44'))
                ->addValidator(new Test_Validate_Mobile())
                ->addValidator('Identical', false, array('token' => 'mobile', 'messages' => 'Mobile numbers do not match.'))
                ->setRequired( true );

        $Add = new Zend_Form_Element_Submit('Add');
        $Add->setLabel('Submit');

        $this->addElement($mobile)
                ->addElement($mobile1)
                ->addElement( $Add );

    }
}


class Test_Validate_Mobile extends Zend_Validate_Abstract
{
    const NOT_DIGITS   = 'notDigits';
    const STRING_EMPTY = 'digitsStringEmpty';
    const INVALID      = 'digitsInvalid';
    const INVALIDPHONE = 'phonenumberinvalid';

    protected static $_filter = null;

    protected $_messageTemplates = array(
        self::NOT_DIGITS   => "'%value%' must contain only digits",
        self::STRING_EMPTY => "'%value%' is an empty string",
        self::INVALID      => "Invalid type given. String, integer or float expected",
        self::INVALIDPHONE => "Invalid number, try with country code",
    );


    public function isValid($value)
    {
        if (!is_string($value) && !is_int($value) && !is_float($value)) {
            $this->_error(self::INVALID);
            return false;
        }

        if (!preg_match('/^(447)[0-9]{9}$/', $value)) {
            $this->_error(self::INVALIDPHONE);
            return false;
        }

        $this->_setValue((string) $value);

        if ('' === $this->_value) {
            $this->_error(self::STRING_EMPTY);
            return false;
        }

        if (null === self::$_filter) {
            require_once 'Zend/Filter/Digits.php';
            self::$_filter = new Zend_Filter_Digits();
        }

        if ($this->_value !== self::$_filter->filter($this->_value)) {
            $this->_error(self::NOT_DIGITS);
            return false;
        }

        return true;
    }
}


class Test_Filter_MobileFilter implements Zend_Filter_Interface
{
    /**
    * Default country code
    *
    * @var string
    */
    protected $_def_country = '44';

    public function __construct($options = null)
    {
        if ($options !== null) {
            if (is_string($options) && is_numeric($options)) {
                $this->_def_country = $options;
            }
            else {
                require_once 'Zend/Filter/Exception.php';
                throw new Zend_Filter_Exception('Options not valid country code');
            }
        }
    }

    public function filter($value)
    {
        if (!empty($value)) {
            $replace = array(' ','+','-','(',')');

            $value = str_replace($replace, '', $value);
            $value = preg_replace('/\A0/', $this->_def_country, $value);

        }
        return $value;
    }

}

我将非常感谢任何帮助。它让我绕弯道。

1 个答案:

答案 0 :(得分:3)

我猜问题是使用Zend_Validate_Identical验证器,它使用来自$ _POST的未过滤原始值。快速修复:

class My_Validate_Identical extends Zend_Validate_Identical
{
    /**
     *
     * @var Zend_Form_Element 
     */
    protected $_element = null;

    public function __construct($token = null)
    {
        if (is_array($token) && array_key_exists('element', $token)) {
            $this->_element = $token['element'];
        }
        parent::__construct($token);
    }
    public function isValid($value, $context = null)
    {
        $context[$this->getToken()] = $this->_element->getValue();
        return parent::isValid($value, $context);
    }    
}

附加验证器:

$mobile1->addValidator(new My_Validate_Identical(array(
                'token' => 'mobile', 
                'messages' => 'Mobile numbers do not match.',
                'element' => $mobile
                )))

希望这会有所帮助:)