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 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值