_view...">

Call assistants from Zend_Form

I am trying to use these codes, but not working:


$this->getView()->translate("Name"); //not work
$this->_view->translate("Name"); //not work
$this->view->translate("Name"); //not work
+3
source share
3 answers

First of all, Zend_Viewit is not introduced in Zend_Form. Therefore, when you call $this->viewor $this->_view, it will not work, because nothing is returned. Why does it work getHelper()? Because it gets the view through a helper broker (and if you use viewRenderer). Take a look at the code below:

// 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 the reason why it $this->_view->translate()works if you call getView()earlier, because it is stored as a protected property.
Accordingly, this code should work fine 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. , . , - Zend_Form, :

Zend_Form::setDefaultTranslator($translator);

.

+6

, , :


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


:


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

+2

The view is not entered in Zend_Form (do not ask me why, when it is required for rendering). You need to extend Zend_Form and introduce a view inside yourself. Another option is to use FrontController-> getInstance ()> getStaticHelper> viewRenderer and get a view from it.

+2
source

Source: https://habr.com/ru/post/1792832/


All Articles