如何验证Zend Framework 2是否存在'email'

时间:2015-08-12 20:03:51

标签: zend-framework2

我想使用NoRocordExists来验证电子邮件是否存在,然后在mysql数据库中插入信息,但我不知道如何调用$ dbapater。

这是我的inputfilter类的代码

 $norecord_exists = new NoRecordExists( 
    array( 
        'table'     => 'users', 
        'field'     => 'email', 
        'adapter'   => $dbadapter 
    ) 
); 
$norecord_exists->setMessage('Email already exists !', 'recordFound'); 
$this->add(array(
    'name' => 'email',
    'required' => true,
    'filters' => array(
        array('name' => 'StringTrim'),
    ),  
    'validators' => array(
        $norecord_exists, 
        array(
            'name'=>'EmailAddress',
            'options'=> array(
                'allowWhiteSpace'=>true,
                'messages' => array(
                    \Zend\Validator\EmailAddress::INVALID_HOSTNAME=>'Email incorrecto',
                ),
            ),
        ),
    )
));

2 个答案:

答案 0 :(得分:1)

使用ZF2,我建议你像这样使用FactoryInterface:

<强> UserFormFactory.php

<?php

namespace User\Form\Service;

use User\Form\UserForm;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

class UserFormFactory implements FactoryInterface
{
    /**
     * @param  ServiceLocatorInterface $serviceLocator
     * @return UserForm
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        /* @var ServiceLocatorInterface $sl */
        $sl = $serviceLocator->getServiceLocator();

        $form = new UserForm();
        $form->setDbAdapter($sl->get('Zend\Db\Adapter\Adapter'));
        return $form;
    }
}

<强> UserForm.php

<?php

namespace User\Form;

use Zend\Db\Adapter\AdapterInterface;
use Zend\Form\Form;
use Zend\InputFilter\InputFilterProviderInterface;

class UserForm extends Form implements InputFilterProviderInterface
{
    /**
     * @var AdapterInterface
     */
    protected $dbAdapter;

    /**
     * Initialisation
     */
    public function init()
    {
        $this->add([
            'name' => 'email',
            'type' => 'Email',
            'options' => [
                'label' => 'Email',
            ],
            'attributes' => [
                'class' => 'form-control',
                'required' => 'required',
            ],
        ]);
        // ...

        $this->add([
            'name' => 'submit',
            'type' => 'Submit',
            'attributes' => [
                'value' => 'Connexion',
                'class' => 'btn btn-default',
            ],
        ]);
    }

    /**
     * InputFilter
     *
     * @return array
     */
    public function getInputFilterSpecification()
    {
        return [
            'email' => [
                'required' => true,
                'filters' => [
                    ['name' => 'StripTags'],
                    ['name' => 'StringTrim'],
                    ['name' => 'StringToLower'],
                ],
                'validators' => [
                    [
                        'name' => 'EmailAddress',
                    ], [
                        'name' => 'Db\NoRecordExists',
                        'options' => [
                           'table' => 'user',
                           'field' => 'email',
                           'adapter' => $this->getDbAdapter(),
                       ],
                    ],
                ],
            ],
            // ...
        ];
    }

    /**
     * @return AdapterInterface
     */
    public function getDbAdapter()
    {
        return $this->dbAdapter;
    }

    /**
     * @param  AdapterInterface $dbAdapter
     * @return UserForm
     */
    public function setDbAdapter(AdapterInterface $dbAdapter)
    {
        $this->dbAdapter = $dbAdapter;
        return $this;
    }
}

<强> module.config.php

return [
    'form_elements' => [
        'factories' => [
            'UserForm' => 'User\Form\Service\UserFormFactory',
        ],
    ],
];

最后,在你的控制器中

$form = $this->getServiceLocator('FormElementManager')->get('UserForm');
//..
if ($form->isValid()) // ...

答案 1 :(得分:0)

您需要移动此代码

$norecord_exists = new NoRecordExists( 
                    array( 
                        'table'     => 'users', 
                        'field'     => 'email', 
                        'adapter'   => $dbadapter 
                    ) 
); 

$norecord_exists->isValid(EMAIL_FROM_THE_FORM_FIELD) {
    return false; //email exists
}
return true; // email doen't exists

在您的控制器或单独的服务/工厂中。 $dbadapter通常会将实例保存到您的Zend\Db\Adaptr\Adapter或您拥有的任何其他配置中。