3

如何将“提交”按钮连接到控制器操作,所以当我按下“提交”时,我的控制器将触发特定操作?

我的控制器:

class MyController extends AbstractActionController
{
    public function viewAction()
    {
        $form = new MyForm();
        $viewModel = new ViewModel(array('form' => $form));
        $viewModel->setTemplate('myForm');

        return $viewModel;
    }

    public function submitAction()
    {
       // want to trigger this
    }
}

我的表格:

class MyForm extends Form
{
    public function __construct()
    {
        parent::__construct('SubmitForm');
        $this->setAttribute('method', 'post');


        $this->add(array(   'name'      => 'submit',
                            'type'      => 'Zend\Form\Element\Submit',
                            'attributes' => array('type'    => 'submit',
                                                  'value'   => 'Submit',
                                                  'id'      => 'submitButton'),
        ));
    }

}

myForm.phtml:

<?php 

$form = $this->form;
$form->prepare();

echo $this->form()->openTag($form);
echo $this->formSubmit($form->get('submit'));    // I want pressing this element should route to MyController::submitAction...
echo $this->form()->closeTag();

?>
4

1 回答 1

3

您可以使用:

$form->setAttribute('action', $url);

您可以$url通过使用 Url 助手获得:

  • 在视图中 -$this->url('my-route');
  • 在控制器中 -$this->url()->fromRoute('my-route');
于 2013-02-06T13:44:45.810 回答