我有一個主頁,我想要一個註冊表單,爲此我正在使用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>
我知道我不應該傳遞口令明文,我的下一個問題是如何處理這個問題。
你爲什麼使用視圖助手?你的觀點幫手在這裏的目的是什麼? – Liyali 2012-04-17 17:12:14
,因爲我不想在名爲register的頁面中註冊表單我希望在我的應用程序的主頁上。 – Rik89 2012-04-17 17:50:25