_view->translate(\"Name\"); //not work" />
开发者

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


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.


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");


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜