使用视图助手渲染表单

时间:2012-04-17 16:46:52

标签: zend-framework zend-form zend-view

我有一个主页,我想要一个注册表单,为此我正在使用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>

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

1 个答案:

答案 0 :(得分: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解决。