Zend框架2 - 如何使用简单数组绑定表单数据

时间:2013-11-27 23:24:08

标签: zend-framework2 zend-form

我正在寻找如何仅使用数组绑定表单数据的简洁解决方案? 我试图转换为没有结果的stdClass对象。

表格

class Product extends Form
{

  public function __construct()
  {
    parent::__construct('add_product');

    $this->setAttribute('method', 'post');

    $this->add(array(
        'name' => 'type',
        'type' => 'Select',
        'options' => array(
            'label' => 'Product type:',
            'value_options' => $this->getProductTypeCollection(),
            //'empty_option' => '',
        ),
        'attributes' => array(
            'value' => 'simple',
            'class' => 'select'
        )
    ));

    $this->add(array(
        'name' => 'set',
        'type' => 'Select',
        'options' => array(
            'label' => 'Product attribute set:',
            'value_options' => $this->getAttributeSetCollection(),
        ),
        'attributes' => array(
            'class' => 'select'
        )
    ));

    $this->add(array(
        'name' => 'categories',
        'type' => 'MultiCheckbox',
        'options' => array(
            'label' => 'Categories:',
            'value_options' => $this->getCategoryTree(),
        ),
        'attributes' => array(
            'class' => 'multi-checkbox'
        )
    ));

    $this->add(array(
        'name' => 'websites',
        'type' => 'Text',
        'options' => array(
            'label' => 'Website:',
        ),
        'attributes' => array(
            'class' => 'text',
            'readonly' => 'readonly',
            'value' => $session->magento_store_view_code,
        )
    ));
  }
}

在编辑操作中:

$form = new ProductForm();
$form->bind($productData);

$ productData是一个数组,它看起来像这样:

array (size=52)
    'product_id' => string '6' (length=1)
    'sku' => string '10/10/1992' (length=10)
    'set' => string '4' (length=1)
    'type' => string 'simple' (length=6)
    'categories' => 
        array (size=1)
          0 => string '3' (length=1)
    'websites' => 
        array (size=1)
          0 => string '1' (length=1)
    'type_id' => string 'simple' (length=6)
    'name' => string 'Front fog lights set crystal smoked VW Golf MK2, Jetta MK2' (length=58)
    'description' => string 'Front fog lights set crystal smoked VW Golf MK2, Jetta MK2' (length=58)
    'short_description' => string 'Front fog lights set crystal smoked VW Golf MK2, Jetta MK2' (length=58)
    'weight' => string '2.0000' (length=6)
    'old_id' => null
    'news_from_date' => null
    'news_to_date' => null
    'status' => string '1' (length=1)
    'url_key' => string 'front-fog-lights-set-crystal-smoked-vw-golf-mk2-jetta-mk2' (length=57)
    'url_path' => string 'front-fog-lights-set-crystal-smoked-vw-golf-mk2-jetta-mk2.html' (length=62)
    'visibility' => string '4' (length=1)
    'category_ids' => 
            array (size=1)
                    0 => string '3' (length=1)
    'required_options' => string '0' (length=1)
    'price' => string '49.9900' (length=7)
    ...

此致

1 个答案:

答案 0 :(得分:0)

更改此

        $form = new ProductForm();
        $form->bind($productData);

到这个

      $product = new Product(); 
      $product->exchangeArray(get_object_vars($productData));
      $form=new ProductForm();
      $form->bind($product);   
      $form->get('submit')->setValue('Save');