Populate a HTML form with Zend Framework session

2019-08-03 19:01发布

问题:

I'm trying to populate a HTML form with data saved in session. This is what i've done so far but it's not working:

In my controller:

      $sessionErrorForm = new Zend_Session_Namespace('errorForm');

      $sessionErrorForm->prenom     = $form['prenom'];

      $this->_redirect('/inscription');

In my view, i need to display something in the value field if a session exists:

 <div><input type="text" name="prenom" value="<?php if (isset($sessionErrorForm->prenom)): echo $sessionErrorForm->prenom; endif;?>" title="Pr&#233;nom *"/>

Thanks in advance for your help

回答1:

Ok. Same thing like here. You always have to instantiate your Session before you can access it. You can declare it in your controller:

$session = new Zend_Session_Namespace('errorForm');
$this->view->sessionErrorForm = $session;

And access it in your view with $this->sessionErrorForm:

<div><input type="text" name="prenom" value="<?php if (isset($this->sessionErrorForm->prenom)): echo $this->sessionErrorForm->prenom; endif;?>" title="Pr&#233;nom *"/>

Or you can declare it in your view and use it like the following:

<?php $sessionErrorForm = new Zend_Session_Namespace('errorForm'); ?>    
<div><input type="text" name="prenom" value="<?php if (isset($sessionErrorForm->prenom)): echo $sessionErrorForm->prenom; endif;?>" title="Pr&#233;nom *"/>

But nevertheless if you write into your session in a previous request you always have to create a new reference to your session, if you don't declare it in the scope before.