开发者

Zend: get Url parameter from inside Zend_Form

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


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


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 :)


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

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜