我想知道是否有办法将其他参数传递给构造函数(首选)或检索Request对象以检查Form构造函数中的头文件,这样当我在控制器中执行getForm时,表单将根据它的调用方式进行定制?
我正在努力将AngularJs绑定和模型标记集成到我的表单元素中,但是我需要修改提交按钮的工作方式,只要从Ajax调用表单并通过框架将其拉入Zend模板。
因此我想在表单中添加提交按钮的位置周围抛出条件参数,但我需要知道渲染的表单是在zend中查看还是通过ajax调用发送。我可以通过查看带有isXmlHttpRequest()的请求头来检测控制器中的ajax调用,但我不知道如何让表单知道控制器在使用$ this-> getForm()检索表单时看到的内容
答案 0 :(得分:0)
您可以使用工厂类注入任何您喜欢的选项。
use MyModule\Form\MyForm;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\FactoryInterface;
class MyFormFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $formElementManager)
{
$serviceManager = $formElementManager->getServiceLocator();
$request = $serviceManager->get('Request');
// I would recommend assigning the data
// from the request to the options array
$options = [
'is_ajax' => $request->isXmlHttpRequest(),
];
// Although you could also pass in the request instance into the form
return new MyForm('my_form', $options, $request);
}
}
如果您注入请求,则需要修改MyForm::__construct
。
namespace MyModule\Form;
use Zend\Form\Form;
use Zend\Http\Request as HttpRequest;
class MyForm extends Form
{
protected $request;
public function __construct($name, $options, HttpRequest $request)
{
$this->request = $request;
parent::__construct($name, $options);
}
}
更新您的module.config.php
以使用工厂
return [
'form_elements' => [
'factories' => [
'MyModule\Form\MyForm' => 'MyModule\Form\MyFormFactory'
]
]
]
然后确保您从服务管理器(在控制器工厂中)请求表单
$myForm = $serviceManager->get('FormElementManager')->get('MyModule\Form\MyForm');
答案 1 :(得分:0)
我的带有辅助函数的AbstractForm(我刚刚将getRequest()添加到底部)。当然,在更广泛的应用程序中,我可能会添加错误检查以确保不会从构造函数中调用它们(当服务管理器尚不可用时)
namespace Application\Form;
use Zend\Form\Form as ZendForm;
use Zend\Http\Request as HttpRequest;
use Zend\Stdlib\RequestInterface as Request;
use Zend\Form\FormElementManager as ZendFormElementManager;
use Zend\ServiceManager\ServiceLocatorAwareInterface as ZendServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface as ZendServiceLocatorInterface;
use Doctrine\ORM\EntityManager as DoctrineEntityManager
class AbstractForm extends ZendForm implements ZendServiceLocatorAwareInterface {
/**
* @var Request
*/
protected $request;
/**
* in form context this turns out to be Zend\Form\FormElementManager
*
* @var ZendFormElementManager $service_manager
*/
protected static $service_manager;
/**
* @var DoctrineEntityManager $entity_manager
*/
protected $entity_manager;
/**
* @var ZendServiceLocatorInterface $service_locator_interface
*/
protected $service_locator_interface;
public function __construct($name = null)
{
parent::__construct($name);
}
/**
* in form context this turns out to be Zend\Form\FormElementManager
*
* @param ZendFormElementManager $serviceLocator
*/
public function setServiceLocator(FormElementManager $serviceLocator)
{
self::$service_manager = $serviceLocator;
}
/**
* in form context this turns out to be Zend\Form\FormElementManager
*
* @return ZendFormElementManager
*/
public function getServiceLocator()
{
return self::$service_manager;
}
/**
* wrapper for getServiceLocator
* @return ZendFormElementManager
*/
protected function getFormElementManager() {
return $this->getServiceLocator();
}
/**
* this returns an actual service aware interface
*
* @return ZendServiceLocatorInterface
*/
protected function getServiceManager() {
if(!($this->service_locator_interface instanceof ZendServiceLocatorInterface)) {
$this->service_locator_interface = $this->getFormElementManager()->getServiceLocator();
}
return $this->service_locator_interface;
}
/**
* @return DoctrineEntityManager
*/
protected function getEntityManager() {
if(!($this->entity_manager instanceof \DoctrineEntityManager)) {
$this->entity_manager = $this->getServiceLocator()->getServiceLocator()->get('Doctrine\ORM\EntityManager');
}
return $this->entity_manager;
}
/**
* Get request object
*
* @return Request
*/
public function getRequest()
{
if (!$this->request) {
$this->request = $this->getServiceManager()->get('Request');
}
return $this->request;
}
}