duannuci4008 2014-01-07 06:27
浏览 24
已采纳

post方法在zend框架中不使用表单装饰器

If I uncomment the below line then I am unable to get the value in $this->_request->getPost('Login')

//$this->setDecorators(array(array('viewScript', array('viewScript' => 'admin/login_decorator.phtml'))));

$this->_request->getPost works fine with forms but not with form decorator.

Below are the files...

forms/loinForm.php

<?php

class Application_Form_LoginForm extends Zend_Form {

    public function __construct($options = null) {
        parent::__construct($options);
        $this->setMethod('post');
        $name = new Zend_Form_Element_Text('username');
        $name->removeDecorator('Label')
                ->removeDecorator("HtmlTag")
                ->addErrorMessage("Please Enter username")
                ->setRequired(true);
        $password = new Zend_Form_Element_Password('password');
        $password->removeDecorator('Label')
                ->removeDecorator("HtmlTag")
                ->addErrorMessage("Please Enter password")
                ->setRequired(true);
        $submit = new Zend_Form_Element_Submit('Login');
        $submit->removeDecorator('Label')
                ->removeDecorator("HtmlTag");

        $this->addElements(array($name, $password, $submit));
        //$this->setDecorators(array(array('viewScript', array('viewScript' => 'admin/login_decorator.phtml'))));
    }

}

views/scripts/admin/login_decorator.phtml

<link href="<?php echo $this->baseUrl(); ?>/css/login.css" media="screen" rel="stylesheet" type="text/css">
<section class="container">
    <div class="login">
      <h1>Login to Administrator</h1>
      <form action="" method="post" enctype="application/x-www-form-urlencoded">
        <p><input type="text" name="username" value="" placeholder="Username"></p>
        <p><input type="password" name="password" value="" placeholder="Password"></p>   
        <p class="submit"><input type="submit" name="commit" value="Login"></p>
      </form>
    </div>

    <div class="login-help">
      <p>Forgot your password? <a href="#">Click here to reset it</a>.</p>
    </div>
  </section>

login.phtml

<?php echo $this->form; ?>

adminController.php loginAction()

public function loginAction() {
        $mysession = new Zend_Session_Namespace('Admin');
        if (isset($mysession->adminName)) {
            $this->_redirect('/admin');
        }
        $form = new Application_Form_loginForm();
        $this->view->form = $form;
        //Preform Admin login action        

        if ($this->_request->getPost('Login')) {
            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) { //If form data is valid
                $name = $this->_request->getPost('username');
                $password = $this->_request->getPost('password');
                /*                 * **Creating object of model adminlogin class**** */
                $adminLoginObj = new Application_Model_Adminlogin();
                $fetchResult = $adminLoginObj->checkAdminAuthority($name, $password);
                if (count($fetchResult) > 0) {
                    $mysession->adminName = $name;
                    $this->_redirect('/admin/');
                } else {
                    $mysession->failLogin = "Invalid Username or Password!";
                    $this->_redirect('/admin/login');
                }
            }
        }
    }

I am unable to find out the reason for this issue. Please help me to resolve this. Thanks in advance.

  • 写回答

1条回答 默认 最新

  • doujia7162 2014-01-07 07:00
    关注

    Please check in your controller if ($this->_request->getPost('Login')) { ...}

    And in views/scripts/admin/login_decorator.phtml

    <input type="submit" name="commit" value="Login">
    

    name of the field is not matching with the action.

    Please check it accordingly. I hope this will help you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题