Zend валидатор не работает с элементами формы SocialEngine - PullRequest
0 голосов
/ 13 сентября 2018

Я видел успешное использование валидаторов для элементов формы в коде SocialEngine, поэтому, когда пользователь добавляет неверный ввод, отображается сообщение об ошибке.Например, указав валидатор «NonEmpty» при добавлении нового элемента формы и указав сообщение, отображаемое для этого валидатора, пользователю, который отправит пустое значение для элемента, будет показано сообщение об ошибке.Я использую следующий код для создания формы, а затем я обрабатываю форму в контроллере.Кажется, все хорошо, но я не знаю, почему это не работает.Независимо от того, что вводится, сообщение валидатора не отображается.

    class Advancedsms_Form_ChangePassword extends  Engine_Form {


    public function init() {
        parent::init();                

        $tabIndex = 10;
        $this->setTitle(Zend_Registry::get('Zend_Translate')->_('Change Password'))
                ->setDescription(Zend_Registry::get('Zend_Translate')->_('Enter your new password'))
                -> setAttrib('id', 'change_password')
                -> setAttrib('enctype', 'multipart/form-data')
                ->setAction(Zend_Controller_Front::getInstance()->getRouter()->assemble([]));

        $this->addElement('Password', 'password', [
            'label'=> Zend_Registry::get('Zend_Translate')->_('New Password'),
            'description' => Zend_Registry::get('Zend_Translate')->_('Enter Your New Password'),
            'required' => true,   
            'validators' => [
                ['NotEmpty', true],
                ['StringLength', true,[8,32]],
                ['Regex', true, ['/^[a-zA-Z0-9_$/i']],
            ],
            'tabindex' => $tabIndex++,
        ]);

        $this->password->getValidator('NotEmpty')->setMessage(Zend_Registry::get('Zend_Translate')->_('Phone number cannot be empty'),'isEmpty');
        $this->password->getValidator('StringLength')->setMessage(Zend_Registry::get('Zend_Translate')->_('Phone length is not correct'));


        $this->addElement('Password', 'password_confirm', [
            'label' => Zend_Registry::get('Zend_Translate')->_('Confirm Your New Password'),
            'description' => Zend_Registry::get('Zend_Translate')->_('Confirm Your New Password'),
            'required' => true,
            'validators' => [
                ['NotEmpty', true],
                ['StringLength', true, [8, 32]],
                ['Regex',true, ['/^[a-ZA-Z0-9_$/i']],
            ],
            'tabindex' => $tabIndex++,
        ]);

        $this->addElement('submit', 'submit', [
                'label'=> Zend_Registry::get('Zend_Translate')->_('Submit'),
            'required' => true,                        
            'tabindex' => $tabIndex++,
                ]
                );       
    }
}

действие :

public function changepasswordAction() {
    $passwordForm = new Advancedsms_Form_ChangePassword();
    $this->view->form = $passwordForm;

    //Access Zend_Session_Namespace
    $this->session = new Zend_Session_Namespace('RecoverySession');

    //If it is NOT post, the user has to provide recovery code and phone number
    if (!$this->getRequest()->isPost()) {
        $phone = filter_input(INPUT_GET, 'phone', FILTER_SANITIZE_URL);
        $recovery = filter_input(INPUT_GET, 'recovery', FILTER_SANITIZE_URL);
        $this->session->phone = $phone;
        $this->session->recoveryCode = $recovery;
    }
    if ($this->getRequest()->isPost()) {
        //Check passwords validity
        $params = $this->getRequest()->getPost();
        $password = $params['password'];
        $passwordConfirm = $params['password_confirm'];
        if($password == $passwordConfirm) {
            $this->view->form->addNotice('Passwords match');
        }
        else {
            $this->view->form->addError('Passwords do not match');
        }
    }
...