Zend - form doesn't get validated

2019-09-02 02:20发布

I'm trying to validate a form in the actual form itself, but it doesn't get validated at all. If I type "someemail&*!([]@gmail.com", it doesn't really give a crap and moves on. Form:

class RecoverPasswordForm extends Form {
    public function __construct($options = null) {
        parent::__construct("RecoverPassword");

        $username = new Element("username");
        $username->setLabel("Email");
        $username->setAttributes(
            array('filters' => array(
                array('name' => 'StringTrim'),
                array('name' => 'StripTags')),
                'validators' => array(
                    array(
                        'name' => 'Regex',
                        'options' => array(
                            'pattern' => '/^[a-zA-Z0-9.!#$%&\'*+\/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/',
                            'messages' => array(
                                Regex::NOT_MATCH => 'The email your provided has invalid characters.',
                            ),
                        ),
                        'break_chain_on_failure' => true
                    ),
                    array(
                        'name' => 'EmailAddress',
                        'options' => array(
                            'messages' => array(
                                EmailAddress::INVALID_FORMAT => "Email address is invalid",
                                EmailAddress::INVALID => "",
                                EmailAddress::INVALID_LOCAL_PART => "",
                                EmailAddress::INVALID_HOSTNAME => "",
                                EmailAddress::INVALID_SEGMENT => "",
                                EmailAddress::DOT_ATOM => "",
                                EmailAddress::INVALID_MX_RECORD => "",
                            ),
                        ),
                    ),
                ),
                'label' => 'Email'));
        $this->add(array(
            $username,

        ));
    }
}

How I'm getting (or trying to get) the filters and validators in the controller:

      public function recoverPasswordAction() {
        $this->cache = new Container("cache");
        $request = $this->getRequest();
        $form = new RecoverPasswordForm();
//      $form->get("submit")->setValue("Send mail");
        $this->view->form = $form;
        if ($request->isPost()) {
            $user = new User();
            $form->getInputFilter()->getValues();
            $form->setInputFilter($form->getInputFilter());
            $data = $request->getPost();
            $form->setData($data);
            $username = $data['username'];
            $userData = $this->getServiceLocator()->get("UserTable")->fetchList("`username` LIKE '$username'");
            if (!count($userData) > 0) {
                $this->cache->error = "A user with this email does not exist, plese try again.";
            }
                if ($form->isValid()) {
                    foreach ($userData as $user) {
                        $user->sendMail(6);
                        $this->cache->success = "A message has been send to this email, containing your password.";
                    }
                }
        }
        return $this->view;
    }

2条回答
神经病院院长
2楼-- · 2019-09-02 02:36

Try like this...

$request = $this->getRequest();

$form = new Form();

if($request->isPost()) {
    if($form->isValid($reguest->getPost())) {
        $values = $form->getValues(); //filtered valid array of values

        $username = $form->getValue('username');


    }
}
$this->view->form = $form;
查看更多
老娘就宠你
3楼-- · 2019-09-02 02:44

I think it's because of wrong implementation of validators.

Could you try to implement InputProviderInterface on your form? Basic example is given in the docs. There it is set on an Element, but you can do this on your form too. This interface define getInputSpecification where you have to return an array with filter and validator specification.

查看更多
登录 后发表回答