mikelovelyuk
mikelovelyuk

Reputation: 4152

Validating Top Level Domains with Zend 1

I am having a problem with Validating Top Level Domains. Basically, anything with .tech as the TLD is failing the email validation.

I have inherited this project and don't know Zend very well but I have traced the problem back to the hostname not being valid 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;

Now, I have some local code here;

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' => "[email protected]                                                                                                                                                                                                                                                [email protected]",//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.')))
            )
        ));

If I comment out the line with the last array('messages' => array('emailAddressInvalidFormat' => 'Incorrect email address specified.'))) then this whole validation is avoided. But I don't want to avoid using this. I just want to be able to extend and add .tech or whatever else comes up from genuine clients. How can I do this with Zend?

Upvotes: 0

Views: 363

Answers (1)

Max P.
Max P.

Reputation: 5679

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' => "[email protected]                                                                                                                                                                                                                                                [email protected]",//line breaks don't work on placeholders, have to force line wrap with spaces
            'filters'     => array('StringTrim'),
            'validators' => array(
                array('validator' => 'NotEmpty'),
            )
        ))->addValidator($emailValidator);

Upvotes: 1

Related Questions