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.

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

报告相同问题?

悬赏问题

  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型