Kategorien
PHP Zend Framework

ZF2 Zend Form Validierung: Email-Adressen

Mit Zend_Validator lässt sich eine sehr anspruchsvolle und sichere E-Mail Validierung durchführen, die man mit passenden Fehlermeldungen für den User bei Bedarf versehen kann:

    <?php

    use Zend\Form\Element;
    use Zend\InputFilter\InputProviderInterface;
    use Zend\Validator;

    class EmailElement extends Element implements InputProviderInterface
    {
        protected $attributes = array(
            'type' => 'email',
        );
        private  $wrongEmailMessage = 'Bitte geben Sie eine gültige E-Mail-Adresse an.';

        public function getInputSpecification()
        {
            return array(

                'name' => $this->getName(),
                'filters' => array(
                    array('name' => 'Zend\Filter\StringTrim'),
                ),
                'attributes' => array(
                    'type' => 'email'
                ),

                'validators' => array(
                    array(
                        'name' => 'NotEmpty',
                        'options' => array(
                            'messages' => array(
                                \Zend\Validator\NotEmpty::IS_EMPTY => $this->wrongEmailMessage
                            ),
                        ),
                    ),

                    array(
                        'name' => 'EmailAddress',
                        'options' => array(
                            'hostnameValidator' => null,
                            'messages' => array(
                                \Zend\Validator\EmailAddress::INVALID => $this->wrongEmailMessage,
                                \Zend\Validator\EmailAddress::INVALID_FORMAT => $this->wrongEmailMessage,
                                \Zend\Validator\EmailAddress::INVALID_HOSTNAME => $this->wrongEmailMessage,
                                \Zend\Validator\Hostname::CANNOT_DECODE_PUNYCODE  => $this->wrongEmailMessag,
                                \Zend\Validator\Hostname::INVALID  => $this->wrongEmailMessage,
                                \Zend\Validator\Hostname::INVALID_DASH  => $this->wrongEmailMessage,
                                \Zend\Validator\Hostname::INVALID_HOSTNAME  => $this->wrongEmailMessage,
                                \Zend\Validator\Hostname::INVALID_HOSTNAME_SCHEMA  => $this->wrongEmailMessage,
                                \Zend\Validator\Hostname::INVALID_LOCAL_NAME  => $this->wrongEmailMessage,
                                \Zend\Validator\Hostname::INVALID_URI  => $this->wrongEmailMessage,
                                \Zend\Validator\Hostname::IP_ADDRESS_NOT_ALLOWED  => $this->wrongEmailMessage,
                                \Zend\Validator\Hostname::UNDECIPHERABLE_TLD  => $this->wrongEmailMessage,
                                \Zend\Validator\Hostname::UNKNOWN_TLD  => $this->wrongEmailMessage,
                                \Zend\Validator\Hostname::LOCAL_NAME_NOT_ALLOWED  => $this->wrongEmailMessage,
                                \Zend\Validator\EmailAddress::INVALID_LOCAL_PART => $this->wrongEmailMessage,
                                \Zend\Validator\EmailAddress::INVALID_MX_RECORD => $this->wrongEmailMessage,
                                \Zend\Validator\EmailAddress::INVALID_SEGMENT => $this->wrongEmailMessage,
                                \Zend\Validator\EmailAddress::DOT_ATOM => $this->wrongEmailMessage,
                                \Zend\Validator\EmailAddress::QUOTED_STRING => $this->wrongEmailMessage,
                                \Zend\Validator\EmailAddress::INVALID_LOCAL_PART => $this->wrongEmailMessage,
                            )
                        )
                    )
                )
            );
        }
    }

Der Validator lässt sich im Zend Framework 2 folgender Maßen anwenden auf ein Form Element:

use Zend\Form\Element;
use Zend\Form\Form;
use Zend\InputFilter\InputFilter;
use Zend\Validator;
use Zend\InputFilter\Factory as InputFilterFactory;

class FooForm extends Form {

    public function __construct() {

        // add() can take either an Element/Fieldset instance,
        // or a specification, from which the appropriate object
        // will be built.
       $this->add(new EmailElement('email'));
}

und mit einer passenden Action verarbeiten:

    public function fooAction() {
        $request = $this->getRequest();

        $responseData = array();

        if ($request->isPost()) {
            $form = new \Path\Form\FooForm();            

            if (! $this->doValidate($form, $request)) {