Zend Framework 2:使用formelementmanager传递变量(“选项”)以形成表单 [英] Zend Framework 2: passing variables ("options") to form using formelementmanager

查看:155
本文介绍了Zend Framework 2:使用formelementmanager传递变量(“选项”)以形成表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以编程方式更改基于某些选项的表单行为。比方说,例如,我正在显示带有用户信息的表单。



当且仅当用户尚未收到激活邮件时,我需要显示一个复选框发送邮件。以前,在ZF1中,我曾经做过类似于

$ $ $ form $ new MyForm(array(displaySendMail=> true ))

反过来,它是作为一个选项被接收的,并且允许做

  class MyForm extends Zend_Form {
$ b $ protected $ displaySendMail;

[...]

public function setDisplaySendMail($ displaySendMail)
{
$ this-> displaySendMail = $ displaySendMail;



public function init(){
[....]
if($ this-> displaySendMail)
{
$ displaySendMail new Zend_Form_Element_Checkbox(sendmail);
$ displaySendMail
- > setRequired(true)
- > setLabel(发送激活邮件);


$ / code>

这怎么可以用Zend Framework 2完成?我发现的所有东西都是关于管理依赖关系(类)的,除此之外没有任何关于这种情况的问题: ZF2如何将变量传递给一个表单
,最后,它会通过一个依赖关系。也许最后一条评论是由Jean Paul Rumeau提供的解决方案,但我无法实现。
Thx
A。

@AlexP,感谢您的支持。我已经使用FormElementManager,所以它应该很简单。如果我理解正确,我应该在我的SomeForm构造函数中检索这些选项,不是吗?

  [在Module.php中] 

'Application \SomeForm'=>函数($ sm)
{
$ form = new SomeForm();
$ form-> setServiceManager($ sm);
返回$ form;
},

在SomeForm.php中

 类SomeForm扩展Form implements ServiceManagerAwareInterface 
{
protected $ sm;

公共函数__construct($ name,$ options){
[这里我有选项?]
parent :: __ construct($ name,$ options);


$ / code $ / pre

我尝试了这个,但没有工作,我们会再次尝试并仔细检查所有内容。

解决方案

使用插件管理器(类扩展 Zend \ServiceManager\AbstractPluginManager )你可以提供'创建选项'数组作为第二个参数。

  $ formElementManager = $ serviceManager-> get('FormElementManager'); 
$ form = $ formElementManager-> get('SomeForm',array('foo'=>'bar'));

重要的是您如何向经理注册服务。 'invokable'服务会将options数组传递到请求的服务的构造函数中,但是工厂(必须是工厂类名称的字符串)将在构造函数中获得选项。 p>

编辑



您已经使用匿名函数注册了您的服务,而不是为你工作。使用工厂 class

  // Module.php 
public function getFormElementConfig )
{
return array($ b $'factories'=> array(
'Application \SomeForm'=&'Application \SomeFormFactory',
) ,
);
}

然后是工厂注入到它的构造函数中的选项(如果你认为它是有意义的)。

  namespace Application; 

使用Application \SomeForm;
使用Zend\ServiceManager\ServiceLocatorInterface;
使用Zend\ServiceManager\FactoryInterface;

类SomeFormFactory实现FactoryInterface
{
protected $ options = array();

public function __construct(array $ options = array())
{
$ this-> options = $ options;


public function createService(ServiceLocatorInterface $ serviceLocator)
{
return new SomeForm('some_form',$ this-> options);


$ / code>

或者,你可以直接注入你所服务的通过将其注册为'invokeable'服务来请求( SomeForm );显然这将取决于服务所需的依赖关系。


I need to programmatically change the behaviour of a form based on some options. Let's say, for example, I'm displaying a form with some user's info.

I need to display a checkbox, "send mail", if and only if a user has not received an activation mail yet. Previously, with ZF1, i used to do something like

$form = new MyForm(array("displaySendMail" => true))

which, in turn, was received as an option, and which allow'd to do

class MyForm extends Zend_Form {

    protected $displaySendMail;

    [...]

    public function setDisplaySendMail($displaySendMail)
    {
        $this->displaySendMail = $displaySendMail;
    }


    public function init() {
        [....]
        if($this->displaySendMail)
        {
            $displaySendMail  new Zend_Form_Element_Checkbox("sendmail");
            $displaySendMail
                   ->setRequired(true)
                   ->setLabel("Send Activation Mail");
        }
    }

How could this be accomplished using Zend Framework 2? All the stuff I found is about managing dependencies (classes), and nothing about this scenario, except this SO question: ZF2 How to pass a variable to a form which, in the end, falls back on passing a dependency. Maybe what's on the last comment, by Jean Paul Rumeau could provide a solution, but I wasn't able to get it work. Thx A.

@AlexP, thanks for your support. I already use the FormElementManager, so it should be straightforward. If I understand correctly, I should just retrieve these option in my SomeForm constructor, shouldn't I?

[in Module.php]

'Application\SomeForm' => function($sm)
           {
                $form = new SomeForm();
                $form->setServiceManager($sm);
                return $form;
            },

while in SomeForm.php

class SomeForm extends Form implements ServiceManagerAwareInterface
{
    protected $sm;

    public function __construct($name, $options) {
         [here i have options?]
         parent::__construct($name, $options);
    }
 }

I tryed this, but was not working, I'll give it a second try and double check everything.

解决方案

With the plugin managers (classes extending Zend\ServiceManager\AbstractPluginManager) you are able to provide 'creation options' array as the second parameter.

$formElementManager = $serviceManager->get('FormElementManager');
$form = $formElementManager->get('SomeForm', array('foo' => 'bar')); 

What is important is how you have registered the service with the manager. 'invokable' services will have the options array passed into the requested service's constructor, however 'factories' (which have to be a string of the factory class name) will get the options in it's constructor.

Edit

You have registered your service with an anonymous function which mean this will not work for you. Instead use a factory class.

// Module.php
public function getFormElementConfig()
{
    return array(
        'factories' => array(
            'Application\SomeForm' => 'Application\SomeFormFactory',
        ),
    );
}

An then it's the factory that will get the options injected into it's constructor (which if you think about it makes sense).

namespace Application;

use Application\SomeForm;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\ServiceManager\FactoryInterface;

class SomeFormFactory implements FactoryInterface
{
    protected $options = array();

    public function __construct(array $options = array())
    {
        $this->options = $options;
    }

    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        return new SomeForm('some_form', $this->options);
    }
}

Alternatively, you can inject directly into the service you are requesting (SomeForm) by registering it as an 'invokeable' service; obviously this will depend on what dependencies the service requires.

这篇关于Zend Framework 2:使用formelementmanager传递变量(“选项”)以形成表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆