Mohammad Ali Akbari
Mohammad Ali Akbari

Reputation: 10395

Call Helpers From Zend_Form

I try this codes, but not works:


$this->getView()->translate("Name"); //not work
$this->_view->translate("Name"); //not work
$this->view->translate("Name"); //not work

Upvotes: 3

Views: 3453

Answers (3)

Radek
Radek

Reputation: 8376

First of all, Zend_View is not injected into Zend_Form. So when you call $this->view or $this->_view it wont work, because there is nothing to return. Why getHelper() works? Because it fetches view via helper broker (and if your are using viewRenderer). Look below at the code:

// Zend/Form.php
public function getView()
{
    if (null === $this->_view) {
        require_once 'Zend/Controller/Action/HelperBroker.php';
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        $this->setView($viewRenderer->view);
    }

    return $this->_view;
}

This is reason why $this->_view->translate() works if you call getView() before, because it's stored as protected property.
According to this, that code should work perfectly and works for me:

class My_Form extends Zend_Form
{
    public function init() 
    {
        echo $this->getView()->translate('name'); //fires 'translate' view helper and translating value
        //below will also work, because you have view now in _view: getView() fetched it.
        echo $this->_view->translate("another thing");
    }
}

BTW. If your using translate helper to translate labels or names of fields, you don't have to. Will be enough, if you set translator object as a static property of Zend_Form, best in your bootstrap:

Zend_Form::setDefaultTranslator($translator);

And from that moment all fields names and labels will be translated automatically.

Upvotes: 6

Tomáš Fejfar
Tomáš Fejfar

Reputation: 11217

View is not injected into Zend_Form (don't ask me why, when it's required for rendering). You have to extend Zend_Form and inject view inside yourself. Other option is using FrontController->getInstance() > getStaticHelper > viewRenderer and recieve view from it.

Upvotes: 2

Mohammad Ali Akbari
Mohammad Ali Akbari

Reputation: 10395

I don't no why, but when I add this function to my form, it work:


public function init() {
        $this->getView();
    }


this line works:


$this->_view->translate("Name");

Upvotes: 2

Related Questions