-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>

我知道我永远不应该以纯文本形式传递密码,我的下一个问题是如何处理这个问题。

4

1 回答 1

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

您的表单将呈现。

如果您想在站点的每个页面上使用表单(例如注销表单),您将使用视图助手。

终于在聊天中解决了。

于 2012-04-17T17:18:07.680 回答