-1

I have a homepage which I want a registration form on, to do this I am using Zend_View_Helper. However nothing is happening.. I cant see the form I am trying to get on that page.

Here's the 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 '';
        }
    }    
}

Here's the form 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));
}

}

Here's the model '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);
}   
}

The controller '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);
        }

    }
}

}

And finally the view:

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

I am aware that I should never be passing passwords in plain text, my next question is how to handle this.

vascowhite
  • 18,120
  • 9
  • 61
  • 77
Rik89
  • 157
  • 4
  • 22
  • Why are you using a view helper? What's the purpose of your view helper here? – Liyali Apr 17 '12 at 17:12
  • as I don't want the registration form in a page called register I want it on the homepage of my application. – Rik89 Apr 17 '12 at 17:50

1 Answers1

1

Your view helper doesn't appear to be doing anything useful. I'm assuming you want it to render the form. If so it should look like this:-

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

Having said that, you don't need a helper for this really. In your view just put:-

echo $this->form

And your form will render.

You would use a view helper if you wanted a form (say a log out form) on every page in your site.

Finally resolved in chat.

Community
  • 1
  • 1
vascowhite
  • 18,120
  • 9
  • 61
  • 77
  • the reason I want the view helper is because I don't want it in the view of a separate page. I have amended the helper and still nothing. – Rik89 Apr 17 '12 at 17:38
  • I take it your helper is in application/views/helpers/Register.php – vascowhite Apr 17 '12 at 17:39
  • As a check remove the if statement from around return $this->view->form; just to make sure that isn't your problem. – vascowhite Apr 17 '12 at 17:41
  • using echo $this->form in your view renders to the current page not a seperate one, so I'm not sure what you mean by "I don't want it in the view of a separate page". – vascowhite Apr 17 '12 at 17:43
  • the helper is in that path, I removed the if statement and the form still doesnt show. If I echo the form in the register.phtml file I get the form.. but I dont want it in this file I want it on my homepage of the application if this makes any sense.. Sorry for the confusion – Rik89 Apr 17 '12 at 17:54
  • Well then echo the form in the view of the page you want it in. When you say homepage do you mean index? – vascowhite Apr 17 '12 at 17:58
  • [come to chat](http://chat.stackoverflow.com/rooms/10188/rendering-a-form-with-a-view-helper) – vascowhite Apr 17 '12 at 18:00
  • Your homepage is a page like the others... you can `echo $this->form;` in your homepage, you don't need a view helper, there is nothing wrong with that. – Liyali Apr 17 '12 at 18:01
  • yeah index.. if I echo $this->view->form; in the index page I get the following error: Notice: Trying to get property of non-object – Rik89 Apr 17 '12 at 18:02
  • click on the chat link above then post your full error message there for me – vascowhite Apr 17 '12 at 18:04
  • 1
    not `$this->view->form;` but `$this->form;`. `$this` already represent an instance of the view. – Liyali Apr 17 '12 at 18:06
  • @Liyali I have him in [chat](http://chat.stackoverflow.com/rooms/10188/rendering-a-form-with-a-view-helper) if you have time to continue to help. – vascowhite Apr 17 '12 at 18:09
  • $this->form gives me nothing.. How does it know which form I am talking about though? There is no reference to the register form in any indexController at all I have multiple forms in my site, and I refernce them all in the specified controllers and views that they belong to. In this case I have a register controller but I want the form in a different view than the register view. – Rik89 Apr 17 '12 at 18:11
  • In your controller you have said $this->view->form = $form; – vascowhite Apr 17 '12 at 18:12
  • You are misunderstanding the MVC setup completely. You need to learn how it works. Try chat again – vascowhite Apr 17 '12 at 18:13
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/10189/discussion-between-vascowhite-and-rik89) – vascowhite Apr 17 '12 at 18:14
  • @vascowhite I had left already, it seems that you solved his problem anyway ;) good job. – Liyali Apr 18 '12 at 01:00