Zend Framework 2表单中的空字段集

时间:2014-09-11 16:05:50

标签: php forms zend-framework2

我发现在我的产品表单字段集中设置表单值有些困难。

我的产品实体是:

namespace Product\Entity;

use DateTime; 
use Zend\Stdlib\ArrayObject as ArrayObject;

class Product implements ProductInterface {

    public $id;
    public $uid;
    public $type_id;
    public $attribute_set_id;
    public $attributes;
    public $createdat;
    public $updatedat;

    /**
     * This method get the array posted and assign the values to the table
     * object
     *
     * @param array $data
     */
    public function exchangeArray ($data)
    {
        foreach ($data as $field => $value) {
            $this->$field = (isset($value)) ? $value : null;
        }

        return true;
    }

    /**
     * @return the $id
     */
    public function getId() {
        return $this->id;
    }

    /**
     * @param field_type $id
     */
    public function setId($id) {
        $this->id = $id;
    }

    /**
     * @return the $uid
     */
    public function getUid() {
        return $this->uid;
    }

    /**
     * @param field_type $uid
     */
    public function setUid($uid) {
        $this->uid = $uid;
    }

    /**
     * @return the $type_id
     */
    public function getTypeId() {
        return $this->type_id;
    }

    /**
     * @param field_type $type_id
     */
    public function setTypeId($type_id) {
        $this->type_id = $type_id;
    }

    /**
     * @return the $attribute_set_id
     */
    public function getAttributeSetId ()
    {
        return $this->attribute_set_id;
    }

    /**
     * @param field_type $attribute_set_id
     */
    public function setAttributeSetId ($attribute_set_id)
    {
        $this->attribute_set_id = $attribute_set_id;
    }

    /**
     * @return the $attributes
     */
    public function getAttributes() {
        return $this->attributes;
    }

    /**
     * @param field_type $attributes
     */
    public function setAttributes(ArrayObject $attributes) {
        $this->attributes = $attributes;
    }

    /**
     * @return the $createdat
     */
    public function getCreatedat() {
        return $this->createdat;
    }

    /**
     * @param field_type $createdat
     */
    public function setCreatedat(DateTime $createdat = null) {
        $this->createdat = $createdat;
    }

    /**
     * @return the $updatedat
     */
    public function getUpdatedat() {
        return $this->updatedat;
    }

    /**
     * @param field_type $updatedat
     */
    public function setUpdatedat(DateTime $updatedat = null) {
        $this->updatedat = $updatedat;
    }
}

当我加载产品信息时,我将所有属性都放入ArrayObject:

object(Product\Entity\Product)[1048]
  public 'id' => string '2' (length=1)
  public 'uid' => string '22ff2755-1540-4d68-8195-079356c33e22' (length=36)
  public 'type_id' => string '1' (length=1)
  public 'attribute_set_id' => string '2' (length=1)
  public 'attributes' => 
    object(Zend\Stdlib\ArrayObject)[1295]
      protected 'storage' => 
        array (size=16)
          0 => 
            object(Product\Entity\ProductAttributes)[1052]
              ...
          1 => 
            object(Product\Entity\ProductAttributes)[1092]
              ...
          2 => 
            object(Product\Entity\ProductAttributes)[1100]
              ...
          3 => 
            object(Product\Entity\ProductAttributes)[1129]
              ...
          4 => 
            object(Product\Entity\ProductAttributes)[1137]
              ...
          5 => 
            object(Product\Entity\ProductAttributes)[1166]
              ...
          6 => 
            object(Product\Entity\ProductAttributes)[1174]
              ...
          7 => 
            object(Product\Entity\ProductAttributes)[1203]
              ...
          8 => 
            object(Product\Entity\ProductAttributes)[1211]
              ...
          9 => 
            object(Product\Entity\ProductAttributes)[1240]
              ...
          10 => 
            object(Product\Entity\ProductAttributes)[1248]
              ...
          11 => 
            object(Product\Entity\ProductAttributes)[1256]
              ...
          12 => 
            object(Product\Entity\ProductAttributes)[1264]
              ...
          13 => 
            object(Product\Entity\ProductAttributes)[1272]
              ...
          14 => 
            object(Product\Entity\ProductAttributes)[1280]
              ...
          15 => 
            object(Product\Entity\ProductAttributes)[1288]
              ...
      protected 'flag' => int 1
      protected 'iteratorClass' => string 'ArrayIterator' (length=13)
      protected 'protectedProperties' => 
        array (size=4)
          0 => string 'storage' (length=7)
          1 => string 'flag' (length=4)
          2 => string 'iteratorClass' (length=13)
          3 => string 'protectedProperties' (length=19)
  public 'createdat' => string '2014-07-08 10:21:40' (length=19)
  public 'updatedat' => string '2014-09-11 11:30:30' (length=19)

这是创建fieldset元素的方法:

/**
 * Prepare the attribute form
 *
 * @param $attributes array           
 * @return \ProductAdmin\Form\ProductForm
 */
public function createAttributesElements(array $attributes) {
    $customHydrator = new ClassMethods (false);
    $parentFilter = new \Zend\InputFilter\InputFilter ();
    $fieldset = new \Zend\Form\Fieldset ( 'attributes' );

    $fieldset->setObject(new \Product\Entity\Product());
    $fieldset->setHydrator ( $customHydrator );
    $fieldset->setFormFactory ( $this->getFormFactory () ); // thanks to jurians #zftalk irc
    $fieldInput = null;

    $inputFilter = new \Zend\InputFilter\InputFilter ();

    foreach ( $attributes as $attribute ) {
        $format = "";
        $filterChain = new \Zend\Filter\FilterChain ();
        $name = $attribute->getName ();
        $label = $attribute->getLabel () ? $attribute->getLabel () : "-";
        $input = $attribute->getInput () ? $attribute->getInput () : "text";
        $type = $attribute->getType () ? $attribute->getType () : "string";
        $isRequired = $attribute->getIsRequired ();
        $sourceModel = $attribute->getSourceModel ();
        $filters = $attribute->getFilters ();
        $validators = $attribute->getValidators ();
        $cssStyles = $attribute->getCss ();
        $validators = ! empty ( $validators ) ? json_decode ( $validators, true ) : array ();

        $filterChain->attachByName ( 'null' ); // set as default

        $inputTypeSource = ! empty ( $sourceModel ) ? $sourceModel : $input;

        // create the new form element array structure
        $formitem = array ('type' => $inputTypeSource, 'name' => $name, 'attributes' => array ('id' => $name ) );

        // set the label of the element
        if (! empty ( $label )) {
            $formitem ['options'] ['label'] = $label;
        }

        // set the css style of the element
        if (! empty ( $cssStyles )) {
            $formitem ['attributes'] ['class'] = $cssStyles;
        } else {
            $formitem ['attributes'] ['class'] = "form-control";
        }

        // Handle the dates
        if (! empty ( $type ) && $type == "date") {
            $customHydrator->addStrategy ( $name, new DateTimeStrategy () );

            $typeSource = 'Zend\Form\Element\Date';
            $formitem ['type'] = "Zend\Form\Element\Date";
            $formitem['options']['format'] = 'd/m/Y';
        }

        // handle the validators preferences of the attribute
        foreach ( $validators as $validator ) {
            $formitem ['validators'] = $validator;
        }

        // var_dump($type);
        // var_dump($customHydrator);
        // var_dump($formitem);

        // Attach the form item into the form
        $fieldset->add ( $formitem );

        $fieldInput = new \Zend\InputFilter\Input ( $name );
        $fieldInput->setRequired ( $isRequired );

        // handle the filters preferences of the attribute
        if (! empty ( $filters )) {

            // get the filters attached to the attribute
            $filters = json_decode ( $filters, true );
            foreach ( $filters as $filter ) {

                // if the filter is an array check it by name
                if (is_array ( $filter )) {

                    // If the filter is a ...
                    if ($filter ['name'] == "File\RenameUpload") {

                        // create the filter Zend\InputFilter\FileInput
                        $thefilter = new \Zend\InputFilter\FileInput ( $filter ['options'] );

                        // ... but how to attach the new filter to the
                        // chain?
                        $chain = new \Zend\Filter\FilterChain ();

                        // ... in this way it doesn't work!!
                        $chain->attachByName ( "filerenameupload", $filter ['options'] );
                        $filterChain->merge ( $chain );

                        // just for debugging it ...
                        $filtersApplied = $filterChain->getFilters ();
                        // var_dump($filtersApplied->toArray());
                    }

                } elseif ($filter == "cleanurl") { // custom filter
                    $filterChain->attach ( new \ProductAdmin\Form\Filter\Cleanurl () );

                } elseif (is_string ( $filter )) {
                    $filterChain->attachByName ( $filter );
                }
            }
        }

        $fieldInput->setFilterChain ( $filterChain );
        $inputFilter->add ( $fieldInput );

    }

    $this->add ( $fieldset );
    $parentFilter->add ( $inputFilter, 'attributes' ); // thanks to GeeH
                                                    // #zftalk irc
    $this->setInputFilter ( $parentFilter );
    return $this;
}

为了更好地解释我的zf2模块,我创建了一个EAV系统(Zend \ Db)来处理产品,并让用户创建他们的产品表格数据结构,如Magento。

Attribute Set

这是产品页面: Product detail page

问题是:为什么当我尝试填充表单时,fieldset为空?

product detail form and its attributes

0 个答案:

没有答案