浏览代码

[Form] Turned FieldGroup::getFields() into 4 specialized methods for more flexibility

It's better to be able to fetch all the visible and all the hidden fields separately for display purposes (hidden fields in <ul> tags without an <li> do not validate)
Jordi Boggiano 14 年之前
父节点
当前提交
d8f4cb79c9
共有 1 个文件被更改,包括 50 次插入3 次删除
  1. 50 3
      src/Symfony/Component/Form/FieldGroup.php

+ 50 - 3
src/Symfony/Component/Form/FieldGroup.php

@@ -198,23 +198,70 @@ class FieldGroup extends Field implements \IteratorAggregate, FieldGroupInterfac
         return $this->fields;
     }
 
+    /**
+     * Returns an array of visible fields from the current schema.
+     *
+     * @return array
+     */
+    public function getVisibleFields()
+    {
+        return $this->getFieldsByVisibility(false, false);
+    }
+
+    /**
+     * Returns an array of visible fields from the current schema.
+     *
+     * This variant of the method will recursively get all the
+     * fields from the nested forms or field groups
+     *
+     * @return array
+     */
+    public function getVisibleFieldsRecursively()
+    {
+        return $this->getFieldsByVisibility(false, true);
+    }
+
     /**
      * Returns an array of hidden fields from the current schema.
      *
+     * @return array
+     */
+    public function getHiddenFields()
+    {
+        return $this->getFieldsByVisibility(true, false);
+    }
+
+    /**
+     * Returns an array of hidden fields from the current schema.
+     *
+     * This variant of the method will recursively get all the
+     * fields from the nested forms or field groups
+     *
+     * @return array
+     */
+    public function getHiddenFieldsRecursively()
+    {
+        return $this->getFieldsByVisibility(true, true);
+    }
+
+    /**
+     * Returns a filtered array of fields from the current schema.
+     *
+     * @param boolean $hidden Whether to return hidden fields only or visible fields only
      * @param boolean $recursive Whether to recur through embedded schemas
      *
      * @return array
      */
-    public function getHiddenFields($recursive = true)
+    protected function getFieldsByVisibility($hidden, $recursive)
     {
         $fields = array();
 
         foreach ($this->fields as $field) {
             if ($field instanceof FieldGroup) {
                 if ($recursive) {
-                    $fields = array_merge($fields, $field->getHiddenFields($recursive));
+                    $fields = array_merge($fields, $field->getFieldsByVisibility($hidden, $recursive));
                 }
-            } else if ($field->isHidden()) {
+            } else if (($hidden && $field->isHidden()) || !$field->isHidden()) {
                 $fields[] = $field;
             }
         }