来自url的Zend 2.x,setValue

时间:2013-08-07 10:35:37

标签: request zend-framework2

我想在Form类中setValue,具体取决于GET请求。要在表单中设置值,请执行以下操作:

class BookingForm extends Form
{
     $this->add(array(
        'name' => 'end',
        'type' => 'Text',
        'options' => array(
            'label' => 'Date to',
        ),
        'attributes' => array(
            'value' => '2013-09-18 15:45',
        ),
    ));
}

但它是硬编码的,我想避免这种情况,是否有方法$this->getRequest()->get('var to be extracted from url') ??

2 个答案:

答案 0 :(得分:0)

我在:

创立了解决方案
class BookingController extends AbstractActionController
{
     ...
     $get = $request->getQuery('start');
     $get = str_replace('_', ' ', $get);
     $form->get('start')->setValue($get);
     ...
 }

就是这样。

答案 1 :(得分:0)

如果您配置了路由器,可以使用:

// router
'index' => array(
  'type' => 'Segment',
  'options' => array(
    'route' => '/myvar/:start',
    'constraints' => array(
      'template' => '[a-zA-Z0-9_-]+'
    ),
    'defaults' => array(
      'controller' => 'module/controller',
      'action' => 'index',
    ),
  ),
),

Controller.php这样

class BookingController extends AbstractActionController
{
     ...
     $get = $this->params("start", "");
     $form->get('start')->setValue($get);
     ...
 }