Awan
Awan

Reputation: 18560

Unable to render Zend Form elements in ViewScript

I have Login.php form like this:

class Form_Login extends Zend_Form {

    public function init() {
    }

    public function __construct(){

        // Set method
        $this->setMethod('post');

        $elements = array();

        $element = $this->createElement('text','username');
        $element->setRequired(true);
        $elements[] = $element;

        $element = $this->createElement('password','password');
        $element->setRequired(true);
        $elements[] = $element;

        $this->addElements( $elements );

        $this->setDecorators( array( array( 'viewScript', array( 'viewScript' => '/authentication/login-form.phtml' ) ) ) );
    }
}

/authentication/login-form.phtml

<table>
    <tr>
        <td><?= $this->getElement('username') ?></td>
    </tr>
    <tr>
        <td><?= $this->getElement('password') ?></td>
    </tr>

</table>

When I render the form thenI get following exception:

Message: Plugin by name 'GetElement' was not found in the registry; used paths: Zend_View_Helper_: Zend/View/Helper/;C:/wamp/www/databox/application/views\helpers/ 

Where I am going wrong. Should I enter any information in application.ini or Bootstrap.

Thanks

Upvotes: 0

Views: 2385

Answers (2)

Samuel Herzog
Samuel Herzog

Reputation: 3611

You need to pass the form to your view script to make it work.

public function someAction()
{
    $this->view->form = new Form_Login();
}

in your view script you can receive the elements via

<table>
<tr>
    <td><?= $this->form->getElement('username'); ?></td>
</tr>
<tr>
    <td><?= $this->form->getElement('password'); ?></td>
</tr>
</table>

Upvotes: 3

V&#225;clav KOzelka
V&#225;clav KOzelka

Reputation: 11

$this->setDecorators( array( array( 'viewScript', array( 'viewScript' =>'/authentication/login-form.phtml','form'=>$form ) ) ) );

and then in view

$this->form->getElement('elementName');

Upvotes: 1

Related Questions