2

Zend talk.I need to set an hidden form field as the value of an url parameter. I have:

class Form_Custom extends Zend_Form
{
  public function init()
  {
   [..] 
   $id = new Zend_Form_Element_Hidden('name');
   [..]
  }
}

I know that I could pass it (from controller) inside an array of options while instantiating the class.But how could I tell the form to look for the URL parameter itself??

Thanks

Luca

luca
  • 36,606
  • 27
  • 86
  • 125

3 Answers3

4
class Form_Custom extends Zend_Form
{
  public function init()
  {
   //[..] 
   $id = new Zend_Form_Element_Hidden('name');
   //[..]
   $this->fill();
  }

  public function fill()
  {
      $this->name->setValue( Zend_Controller_Front::getInstance()->getRequest()->getParam( 'name', null ) );
  }
}

Using construction above you can populate your field when the form is created as well as manually using $form->fill() from controller

Arek Jablonski
  • 349
  • 1
  • 7
1

If you want to pass the URL value to your hidden field, you could use the following method:

In your controller:

    // Get wanted value from url
    $var = $this->_request->getParam('url');

    // Get correct form using Formloader helper (or any other method that you might be using)
    $this->view->form = $this->_helper->FormLoader('formname', array('url'=> $var);

And in your form you add the following:

protected $_url;

public function init(){
    $url = $this->_url;

    /*rest of your form*/
}

public function setUrl($value){
    $this->_url = $value;
}

That's it! Just pass the $url variable in your form the hidden field and you should be all done :)

Jimmy C.
  • 89
  • 2
  • 8
0

I don't think there is a way to do that because Zend_form is dissociated from the controller. You could register in the registry your pos/get variables but thats dirty. You could also access directly $_GET/$_POST...

Or in your controller you can do:

$form = new Zend_form();
$form->populate($this->_getAllParams());

Which will populate your form with all values from $_POST/$_GET

zzarbi
  • 1,832
  • 3
  • 15
  • 29