【问题标题】:populating zend form's fieldsets with objects within object用对象内的对象填充 zend 表单的字段集
【发布时间】:2015-05-25 05:26:00
【问题描述】:

我正在使用 PHP Zendframework 来构建表单。我有我需要用来填充我的 ServiceEditForm.php 的服务对象。但在这种服务形式中,我有“计费”、“订阅”对象和“命令”对象数组。下面是我对服务类的实现。

 class Service{
public $service_id;
public $ServiceName;
public $TelecomOperator;
public $SubMethod;
public $Provider;
public $active;



public $billingType;
public $subscriptionPlan;
public $commands;

function exchangeArray(array $data);}

我想将服务类的对象绑定到我的编辑表单,该表单使用订阅、计费和命令相关数据作为字段集。我能够使用绑定而不是其他对象来填充表单中的服务值。这是我的表单实现

     class ServiceEditForm extends Form{

public function __construct($name = null)
{
    parent::__construct('Edit Service');
    $this->setAttribute('method', 'post');
    $this->setAttribute('enctype','multipart/form-data');

    //here i have other fields that belongs to service object

    $this->add( array(
        'name' => 'billingType',
        'type' => 'Services\Form\BillingTypeFieldset',
        'options' => array(
            'label' => 'Billing Type',
        ),
    ));


    $this->add( array(
        'name' => 'subscriptionPlan',
        'type' => 'Services\Form\SubscriptionPlanFieldset',
        'options' => array(
            'label' => 'Subscription Plan',
        ),
    ));

    $this->add(array(
         'type' => 'Zend\Form\Element\Collection',
         'name' => 'commands',
         'options' => array(
             'label' => 'commands',
             'count' => 2,
             'should_create_template' => true,
             'allow_add' => true,
             'target_element' => array(
                 'type' => 'Services\Form\CommandFieldset',
             ),
         ),
     ));

    $this->add(array(
        'name' => 'submit',
        'attributes' => array(
            'type'  => 'submit',
            'value' => 'Save'
        ),
    )); 
}

}

正如我所说,我无法使用此实现在表单内填充带有绑定的字段集。任何建议将不胜感激。

【问题讨论】:

  • 我能够研究一下并能够解决(但不完全理解背后的概念)。我用 ArraySerializable 替换了 hydrator ClassMethods 并为我做了一个转变。

标签: php data-binding zend-framework2


【解决方案1】:

虽然我在寻找可能的解决方案后已经回答了这个问题,但是这个链接中给出的 Hydrator Implementations 在解释使用它之前应该阅读的基础知识方面也很有用。可惜我在实施字段集之前没有进行全面的研究。

http://framework.zend.com/manual/current/en/modules/zend.stdlib.hydrator.html

这个 url 解释了 hydrators 的可能实现,它可以为你的班级提供非常简单的绑定解决方案。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多