gpt4 book ai didi

php - Zend Framework 2 FormSubmit 调用 Controller 操作

转载 作者:搜寻专家 更新时间:2023-10-31 20:44:38 27 4
gpt4 key购买 nike

如何将“提交”按钮连接到 Controller 操作,以便在我按下“提交”时我的 Controller 将触发特定操作?

我的 Controller :

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'),
));
}

}

我的表格.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();

?>

最佳答案

您可以使用:

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

您可以使用 Url 帮助器获取 $url:

  • 在 View 中 - $this->url('my-route');
  • 在 Controller 中 - $this->url()->fromRoute('my-route');

关于php - Zend Framework 2 FormSubmit 调用 Controller 操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14730256/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com