Auris
Auris

Reputation: 1329

ZF2 EmailAddress validator

I am creating ZF2 email address validator via factory that has 2 parts, one checks if emaill is already in DB, two: validate the email. Prblem is that my NoObjectExists validator works just fine, but the acatual Email address validator does not (validator recognises "dsfsfhsadjkfnaskl" as valid email). Here is my code, maybe you guys can spot what is wrong with it?

    $factory = new \Zend\InputFilter\Factory();

    $input =  $factory->createInput(array(
        'name' => 'email',
        'required' => false,
        'filters' => array(
            0 => array(
                'name' => 'Zend\Filter\StringTrim',
                'options' => array(),
            ),
        ),
        'validators' => array(
            0 => array(
                    'name' => '\DoctrineModule\Validator\NoObjectExists',
                    'options' => array(
                        'object_repository' => $this,
                        'fields' => array('email'),
                ),
            1 => array(
                    'name' => '\Zend\Validator\EmailAddress',
                    'options' => array(
                        'allow' => \Zend\Validator\Hostname::ALLOW_DNS,
                        'domain' => true,
                    ),
                ),
            ),
        ),
    ));

    return $input;

Upvotes: 0

Views: 456

Answers (1)

Purple Hexagon
Purple Hexagon

Reputation: 3578

You have an error in your array nesting. You aren't closing off the containing the NoObjectExists validator so the Email validator is nested inside.

Try the following:

    $factory = new \Zend\InputFilter\Factory();

    $input =  $factory->createInput(array(
        'name' => 'email',
        'required' => false,
        'filters' => array(
            0 => array(
                'name' => 'Zend\Filter\StringTrim',
                'options' => array(),
            ),
        ),
        'validators' => array(
            0 => array(
                'name' => '\DoctrineModule\Validator\NoObjectExists',
                'options' => array(
                    'object_repository' => $this,
                    'fields' => array('email'),
                ),
            ),
            1 => array(
                'name' => '\Zend\Validator\EmailAddress',
                'options' => array(
                    'allow' => \Zend\Validator\Hostname::ALLOW_DNS,
                    'domain' => true,
                ),
            ),
        ),
    ));

    return $input;

Upvotes: 2

Related Questions