Skip to content

Commit

Permalink
[Form] Turned FieldGroup::getFields() into 4 specialized methods for …
Browse files Browse the repository at this point in the history
…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)
  • Loading branch information
Seldaek authored and fabpot committed Oct 6, 2010
1 parent 5019010 commit d8f4cb7
Showing 1 changed file with 50 additions and 3 deletions.
53 changes: 50 additions & 3 deletions src/Symfony/Component/Form/FieldGroup.php
Expand Up @@ -198,23 +198,70 @@ public function getFields()
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;
}
}
Expand Down

0 comments on commit d8f4cb7

Please sign in to comment.