2012-04-17 53 views
-1

我有一個主頁,我想要一個註冊表單,爲此我正在使用Zend_View_Helper。然而,沒有任何事情發生..我不能看到我想要在該頁面上的表單。使用視圖幫助器呈現表單

這裏的幫手 'Register.php':

<?php 

class Zend_View_Helper_Register extends Zend_View_Helper_Abstract { 

    public function register(){ 

    $request = Zend_Controller_Front::getInstance()->getRequest(); 
     $controller = $request->getControllerName(); 
     $action = $request->getActionName(); 
     if ($controller == 'register' && $action == 'index') { 
      return ''; 
     } 
    }  
} 

這裏的形式Register.php:

<?php 

class Application_Form_Register extends Zend_Form 
{ 


    public function init() { 
     $this->setName('register'); 
     $id = new Zend_Form_Element_Hidden('id'); 
    $id->addFilter('Int'); 
    $first_name = new Zend_Form_Element_Text('first_name'); 
    $first_name->setLabel('Firstname') 
      ->setRequired(true) 
      ->addFilter('StripTags') 
      ->addFilter('StringTrim') 
      ->addValidator('NotEmpty'); 
    $surname = new Zend_Form_Element_Text('surname'); 
    $surname->setLabel('Surname') 
      ->setRequired(true) 
      ->addFilter('StripTags') 
      ->addFilter('StringTrim') 
      ->addValidator('NotEmpty'); 
    $email = new Zend_Form_Element_Text('email'); 
    $email->setLabel('Email') 
      ->setRequired(true) 
      ->addFilter('StripTags') 
      ->addFilter('StringTrim') 
      ->addValidator('NotEmpty'); 
    $username = new Zend_Form_Element_Text('username'); 
    $username->setLabel('Username') 
      ->setRequired(true) 
      ->addFilter('StripTags') 
      ->addFilter('StringTrim') 
      ->addValidator('NotEmpty'); 
    $password = new Zend_Form_Element_Text('password'); 
    $password->setLabel('Password') 
      ->setRequired(true) 
      ->addFilter('StripTags') 
      ->addFilter('StringTrim') 
      ->addValidator('NotEmpty'); 
    $age = new Zend_Form_Element_Text('age'); 
    $age->setLabel('Age') 
      ->setRequired(true) 
      ->addFilter('StripTags') 
      ->addFilter('StringTrim') 
      ->addValidator('NotEmpty'); 
    $gender = new Zend_Form_Element_Text('gender'); 
    $gender->setLabel('Gender') 
      ->setRequired(true) 
      ->addFilter('StripTags') 
      ->addFilter('StringTrim') 
      ->addValidator('NotEmpty'); 
    $uni = new Zend_Form_Element_Text('pic_2'); 
    $uni->setLabel('Uni') 
      ->setRequired(true) 
      ->addFilter('StripTags') 
      ->addFilter('StringTrim') 
      ->addValidator('NotEmpty'); 
    $submit = new Zend_Form_Element_Submit('submit'); 
    $submit->setAttrib('id', 'submitbutton'); 
    $this->addElements(array($id, $firstname, $surname, $email, $username, $password, $age, $gender, $uni, $submit)); 
} 

} 

這裏的模型 'Users.php'

<?php 

class Application_Model_DbTable_Users extends Zend_Db_Table_Abstract 
{ 

    protected $_name = 'users'; 

    public function addUser($first_name, $surname, $email, $username, $password, $age, $gender, $uni) { 
     $data = array(
     'first_name' => $first_name, 
     'surname' => $surname, 
     'email' => $email, 
     'username' => $username, 
     'password' => $password, 
     'age' => $age, 
     'gender' => $uni, 
    ); 
    $this->insert($data); 
} 
} 

的控制器'RegisterController.php'

<?php 

class RegisterController extends Zend_Controller_Action 
{ 

    public function init() 
    { 
     /* Initialize action controller here */ 
    } 

public function indexAction() 
{ 
    $form = new Application_Form_Register(); 
    $form->submit->setLabel('Register'); 
    $this->view->form = $form; 
    if ($this->getRequest()->isPost()) { 
     $formData = $this->getRequest()->getPost(); 
     if ($form->isValid($formData)) { 
      $first_name = $form->getValue('first_name'); 
      $surname = $form->getValue('surname'); 
      $email = $form->getValue('email'); 
      $username = $form->getValue('username'); 
      $password = $form->getValue('password'); 
      $age = $form->getValue('age'); 
      $gender = $form->getValue('gender'); 
      $uni = $form->getValue('uni'); 
      $register = new Application_Model_DbTable_Users(); 
      $register->addUser($first_name, $surname, $email, $username, $password, $age, $gender, $uni); 
      $this->_helper->redirector('index'); 
     } else { 
      $form->populate($formData); 
     } 

    } 
} 

} 

最後認爲:

<div id="register"> 
    <h2>Register</h2> 
    <?php echo $this->register(); ?> 
</div> 

我知道我不應該傳遞口令明文,我的下一個問題是如何處理這個問題。

+0

你爲什麼使用視圖助手?你的觀點幫手在這裏的目的是什麼? – Liyali 2012-04-17 17:12:14

+0

,因爲我不想在名爲register的頁面中註冊表單我希望在我的應用程序的主頁上。 – Rik89 2012-04-17 17:50:25

回答

1

您的看法幫手似乎沒有做任何有用的事情。我假設你想讓它呈現表單。如果是這樣,它應該看起來像這樣: -

class Zend_View_Helper_Register extends Zend_View_Helper_Abstract { 

    public function register() 
    { 
     $request = Zend_Controller_Front::getInstance()->getRequest(); 
     $controller = $request->getControllerName(); 
     $action = $request->getActionName(); 
     if ($controller == 'register' && $action == 'index') { 
      return $this->view->form; 
     } 
    }  
} 

話雖如此,你真的不需要幫手。在你看來,只是把:

echo $this->form 

而你的形式將呈現。

如果您想要在網站的每個頁面上使用表單(例如註銷表單),您都可以使用視圖助手。

最終在chat中解決。

+0

我希望視圖助手的原因是因爲我不希望它在單獨的頁面視圖中。我已經修改了幫手,但仍然沒有。 – Rik89 2012-04-17 17:38:09

+0

我把它作爲你的幫手是在application/views/helpers/Register.php – vascowhite 2012-04-17 17:39:55

+0

作爲一個支票從附近移除if語句$ this-> view-> form;只是爲了確保這不是你的問題。 – vascowhite 2012-04-17 17:41:03

相關問題