Skip to content

Commit

Permalink
Merge pull request #210 from sminnee/form-visiblefields
Browse files Browse the repository at this point in the history
Form::VisibleFields() and FieldList::VisibleFields()
  • Loading branch information
Stig Lindqvist committed Mar 9, 2012
2 parents 2b24589 + 3d54668 commit 2ab12af
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 88 deletions.
34 changes: 24 additions & 10 deletions forms/FieldList.php
Expand Up @@ -219,7 +219,7 @@ public function replaceField($fieldName, $newField) {
* @param string $newFieldTitle New title of field
* @return boolean
*/
function renameField($fieldName, $newFieldTitle) {
public function renameField($fieldName, $newFieldTitle) {
$field = $this->dataFieldByName($fieldName);
if(!$field) return false;

Expand Down Expand Up @@ -424,7 +424,7 @@ public function setValues($data) {
*
* @return FieldList
*/
function HiddenFields() {
public function HiddenFields() {
$hiddenFields = new FieldList();
$dataFields = $this->dataFields();

Expand All @@ -434,14 +434,28 @@ function HiddenFields() {

return $hiddenFields;
}

/**
* Return all fields except for the hidden fields.
* Useful when making your own simplified form layouts.
*/
public function VisibleFields() {
$visibleFields = new FieldList();

foreach($this as $field) {
if(!($field instanceof HiddenField)) $visibleFields->push($field);
}

return $visibleFields;
}

/**
* Transform this FieldList with a given tranform method,
* e.g. $this->transform(new ReadonlyTransformation())
*
* @return FieldList
*/
function transform($trans) {
public function transform($trans) {
$this->flushFieldsCache();
$newFields = new FieldList();
foreach($this as $field) {
Expand All @@ -453,12 +467,12 @@ function transform($trans) {
/**
* Returns the root field set that this belongs to
*/
function rootFieldSet() {
public function rootFieldSet() {
if($this->containerField) return $this->containerField->rootFieldSet();
else return $this;
}

function setContainerField($field) {
public function setContainerField($field) {
$this->containerField = $field;
return $this;
}
Expand All @@ -468,7 +482,7 @@ function setContainerField($field) {
*
* @return FieldList
*/
function makeReadonly() {
public function makeReadonly() {
return $this->transform(new ReadonlyTransformation());
}

Expand All @@ -477,7 +491,7 @@ function makeReadonly() {
*
* @param string|FormField
*/
function makeFieldReadonly($field) {
public function makeFieldReadonly($field) {
$fieldName = ($field instanceof FormField) ? $field->getName() : $field;
$srcField = $this->dataFieldByName($fieldName);
$this->replaceField($fieldName, $srcField->performReadonlyTransformation());
Expand All @@ -492,7 +506,7 @@ function makeFieldReadonly($field) {
*
* @param array $fieldNames Field names can be given as an array, or just as a list of arguments.
*/
function changeFieldOrder($fieldNames) {
public function changeFieldOrder($fieldNames) {
// Field names can be given as an array, or just as a list of arguments.
if(!is_array($fieldNames)) $fieldNames = func_get_args();

Expand Down Expand Up @@ -527,7 +541,7 @@ function changeFieldOrder($fieldNames) {
* @param string|FormField
* @return Position in children collection (first position starts with 0). Returns FALSE if the field can't be found.
*/
function fieldPosition($field) {
public function fieldPosition($field) {
if(is_object($field)) $field = $field->getName();

$i = 0;
Expand All @@ -549,7 +563,7 @@ function fieldPosition($field) {
* @subpackage fields-structural
*/
class HiddenFieldList extends FieldList {
function forTemplate() {
public function forTemplate() {
$output = "";
foreach($this as $field) {
$output .= $field->Field();
Expand Down

0 comments on commit 2ab12af

Please sign in to comment.