ZF-11239: Zend_Validate_EmailAddress should instantiate Zend_Validate_Hostname or completely ignore when not set


Currently Zend_Validate_EmailAddress does several calls on $this->options['hostname'] - whether set or not, resulting in fatal error "call to a member function on a non-object". In my opinion, a fatal error should _always be prevented, so either set a default hostname validator or ignore all portions where one would be needed.

Reproduce code:

class FoobarController {
      public function indexAction() {
            $validator = new Zend_Validate_EmailAddress(array(
                  'mx'       => FALSE,
                  'deep'     => FALSE,
                  'domain'   => TRUE,
                  'messages' => array(
                        Zend_Validate_EmailAddress::INVALID             => 'Invalid blabla',
                        // more Zend_Validate_EmailAddress::-messages
                        Zend_Validate_Hostname::CANNOT_DECODE_PUNYCODE  => 'Can not...'

Suggested fix: all calls to $this->_options['hostname'] should be replaced by calls to $this->getHostnameValidator(), which checks for a set validator and instantiates one if requried.

public function getHostnameValidator() {
      if(!(this->_options['hostname'] instanceof Zend_Validate_Hostname)) {
            // Should be enough as it already handles creation when passed NULL
      return $this->_options['hostname'];

Additionally, setOptions() should handle 'messages' option after 'hostname' option so one can pass in a hostname validator and let Validate_EmailAddress set messages to it. The current code would call setMessage() on the hostname validator before it is set or on the default (if the above fix is applied), but never on one specified in the options.

Checked version: SVN rev. 23772


Fixed in ZF2 with GH-324

Backported fix to ZFv1 in r24660 r24661