Php Zend框架验证和/或筛选问题
我对下面表单的筛选和验证有问题。如果两个手机号码以国家格式(07777654321)输入,则表单验证失败,但如果以国际格式(447777654321)输入,则表单验证失败。我的理解是,将首先应用过滤器,因此当应用相同的过滤器时,Php Zend框架验证和/或筛选问题,php,zend-framework,zend-form,Php,Zend Framework,Zend Form,我对下面表单的筛选和验证有问题。如果两个手机号码以国家格式(07777654321)输入,则表单验证失败,但如果以国际格式(447777654321)输入,则表单验证失败。我的理解是,将首先应用过滤器,因此当应用相同的过滤器时,mobile和mobile\u confirm应使用相同的过滤器集进行处理 因此,如果我在两个字段中输入07777654321,它将被过滤到两个字段中的447777654321,然后应该进行验证。我哪里做错了 我使用的是ZF1.11.3 <?php class T
mobile
和mobile\u confirm
应使用相同的过滤器集进行处理
因此,如果我在两个字段中输入07777654321
,它将被过滤到两个字段中的447777654321
,然后应该进行验证。我哪里做错了
我使用的是ZF1.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;
}
}
我猜问题在于Zend_Validate_相同的验证器,它使用来自$\u 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
)))
希望这有帮助:)那么您是否单独检查了过滤,以查看过滤后是否得到所需的值?是的。据我所知,过滤工作正常。验证失败后,表格再次显示,国家格式按预期更改为国际格式。我想它一定很简单。非常感谢您的帮助。很好,这很有帮助,因为您没有提供错误消息,我不确定情况是否如此。。。