使用Zend 1

时间:2016-05-17 10:31:37

标签: php zend-framework

我遇到Validating Top Level Domains的问题。基本上,以.tech作为TLD的任何内容都无法通过电子邮件验证。

我继承了这个项目并且不太了解Zend,但我已经将问题追溯到主机名无效here is the code on GitHub;

    // Match hostname part
    if ($this->_options['domain']) {
        $hostname = $this->_validateHostnamePart();
    }

    $local = $this->_validateLocalPart();

    // If both parts valid, return true
    if ($local && $length) {
        if (($this->_options['domain'] && $hostname) || !$this->_options['domain']) {
            return true;
        }
    }

    return false;

现在,我在这里有一些本地代码;

class Form_InviteToSpaceForm extends Twitter_Bootstrap_Form_Horizontal
{

    public function init()
    {

        // Set the method for the display form to POST
        $this->setMethod('post');
        $this->setAction('/team');

        $this->addElement('textarea', 'email', array(
            'label'       => 'Email addresses',
            'dimension' => 10,
            'required'    => true,
            'placeholder' => "person1@email.com                                                                                                                                                                                                                                                person2@email.com",//line breaks don't work on placeholders, have to force line wrap with spaces
            'filters'     => array('StringTrim'),
            'validators' => array(
                array('validator' => 'NotEmpty'),
                array('validator' => 'EmailAddress', 'options' => array('messages' => array('emailAddressInvalidFormat' => 'Incorrect email address specified.')))
            )
        ));

如果我用最后一个array('messages' => array('emailAddressInvalidFormat' => 'Incorrect email address specified.')))注释掉该行,则可以避免整个验证。但我不想避免使用它。我只是希望能够扩展并添加.tech或其他来自真正客户端的内容。我怎么能用Zend做到这一点?

1 个答案:

答案 0 :(得分:1)

You can write custom validator extended from Zend validator

class My_Validate_Hostname extends Zend_Validate_Hostname
{
    public function __construct($options = array())
    {
        parent::__construct($options);
        $this->_validTlds = array_merge($this->_validTlds, array('tech'));
    }
}

and pass it to email validator

$emailValidator = new Zend_Validate_EmailAddress(array('messages' => array('emailAddressInvalidFormat' => 'Incorrect email address specified.')));
$emailValidator->setHostnameValidator(new My_Validate_Hostname());
....
$this->addElement('textarea', 'email', array(
            'label'       => 'Email addresses',
            'dimension' => 10,
            'required'    => true,
            'placeholder' => "person1@email.com                                                                                                                                                                                                                                                person2@email.com",//line breaks don't work on placeholders, have to force line wrap with spaces
            'filters'     => array('StringTrim'),
            'validators' => array(
                array('validator' => 'NotEmpty'),
            )
        ))->addValidator($emailValidator);