doulierong0334 2014-08-12 11:43 采纳率: 100%
浏览 31
已采纳

使用setAction时,Zend表单验证失败

I would like to validate form and if validation passes it has to be submitted to connectTracking Action.

At present form gets submitted to connectTracking action before it passes validation.

Controller:

public function easyAction(){
    $auth = Zend_Auth::getInstance();
    $user_id = $this->_helper->Utilities->getCurrentUserId();

    if ($auth->hasIdentity()) {
        $form       = new Application_Form_easy();
        $form->submit->setLabel('Submit');
        $this->view->form = $form;  
        $formData = $this->getRequest()->getPost();

        if ( ($this->getRequest()->getPost('easyform', false)) &&  ($form->isValid($this->getRequest()->getPost())) ) { 
            $username = $form->getValue('username');                
            $password = $form->getValue('password');
            $releasedata = array('username' => $username, 'password' => $password);
        }
    }
}

/application/forms/easy.php

class Application_Form_easy extends Zend_Form {
    public function init() {
        $this->setMethod('post');
        $this->setName('easyform');
        $this->setAction('/index.php/releases/connectTracking'); # Submitting to different action

        $username = new Zend_Form_Element_Text('username');
        $username->setLabel('Username * :')
                ->setRequired(true)
                ->addValidator('NotEmpty', true, array('messages' => 'Please enter Username'));

        $password = new Zend_Form_Element_Password('password');
        $password->setLabel('Password * :')
                ->setRequired(true)
                ->addValidator('NotEmpty', true, array('messages' => 'Please enter password '));

                $submit = new Zend_Form_Element_Submit('submit');
                $submit->setAttrib('id', 'easyform');
                $submit->setAttrib('name', 'easyform');
                $this->addElements(array($username, $password, $submit));
    }
}

Could you please let me know how shall I validate the form and then submit to action

Many Thanks!

  • 写回答

1条回答 默认 最新

  • dpi74187 2014-08-12 11:56
    关注

    Basically isValid is the method to validate a form:

    $form = new Application_Form_easy();
    $formData = $this->_request->getPost();
    if($form->isValid($formData)){
     //Form is valid, do your stuff
    } else {
     //Form is not valid re-populate data
     $form->populate($formData);
    }
    

    In your case you are setting the form action this one /index.php/releases/connectTracking and you have written your validation code in easyAction which is wrong. you have two ways here to achieve your requirement;

    1) Set action of your form this /controllerName/easy and validate your form in easyAction (which you are doing right now) and if form is valid then redirect control to releases/connectTracking action.

    2) Another way is to write your validation code in releases/connectTracking Action and if form is not valid then redirect control again to easy Action with all the POST data to re-populate the form. And if form is valid then continue your stuff at releases/connectTracking action.

    E.g. for first solution:

    Controller:

    public function easyAction(){
        $auth = Zend_Auth::getInstance();
        $user_id = $this->_helper->Utilities->getCurrentUserId();
    
        if ($auth->hasIdentity()) {
            $form       = new Application_Form_easy();
            $form->submit->setLabel('Submit');
            $this->view->form = $form;  
            $formData = $this->getRequest()->getPost();
    
            if ($form->isValid($formData)) { 
                $username = $form->getValue('username');                
                $password = $form->getValue('password');
                $releasedata = array('username' => $username, 'password' => $password);
                //Form is valid, hence forward controller to another action with formData
                $this->_request->setPost(array('formData' => $formData));
                //Write actual module name in the below line
                $this->_forward('connectTracking', 'releases', 'moduleName');
                //$this->_redirect('/releases/connectTracking');
            } else {
              $form->populate($formData);
            }
        }
    }
    

    Form:

    class Application_Form_easy extends Zend_Form {
        public function init() {
            $this->setMethod('post');
            $this->setName('easyform');
            //Write actual name of your controller here
            $this->setAction('/controllerName/easy'); # Submitting to same action
    
            $username = new Zend_Form_Element_Text('username');
            $username->setLabel('Username * :')
                    ->setRequired(true)
                    ->addValidator('NotEmpty', true, array('messages' => 'Please enter Username'));
    
            $password = new Zend_Form_Element_Password('password');
            $password->setLabel('Password * :')
                    ->setRequired(true)
                    ->addValidator('NotEmpty', true, array('messages' => 'Please enter password '));
    
                    $submit = new Zend_Form_Element_Submit('submit');
                    $submit->setAttrib('id', 'easyform');
                    $submit->setAttrib('name', 'easyform');
                    $this->addElements(array($username, $password, $submit));
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 bywave配置文件写入失败
  • ¥20 基于Simulink的ZPW2000轨道电路仿真
  • ¥15 pycharm找不到在环境装好的opencv-python
  • ¥15 在不同的执行界面调用同一个页面
  • ¥20 基于51单片机的数字频率计
  • ¥50 M3T长焦相机如何标定以及正射影像拼接问题
  • ¥15 keepalived的虚拟VIP地址 ping -s 发包测试,只能通过1472字节以下的数据包(相关搜索:静态路由)
  • ¥15 Mabatis查询数据
  • ¥15 想知道lingo目标函数中求和公式上标是变量情况如何求解
  • ¥15 关于E22-400T22S的LORA模块的通信问题