Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

API CHANGE: Added Form::VisibleFields() and FieldList::VisibleFields(…

…), which list everything except hidden fields, to assist with the creation of custom form layouts.
  • Loading branch information...
commit ba93028b01a4bbf98c2ab81d726c8ba1178c3325 1 parent ae45ef3
@sminnee sminnee authored
View
14 forms/FieldList.php
@@ -434,6 +434,20 @@ function HiddenFields() {
return $hiddenFields;
}
+
+ /**
+ * Return all fields except for the hidden fields.
+ * Useful when making your own simplified form layouts.
+ */
+ 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,
View
8 forms/Form.php
@@ -543,6 +543,14 @@ function Fields() {
function HiddenFields() {
return $this->fields->HiddenFields();
}
+
+ /**
+ * Return all fields except for the hidden fields.
+ * Useful when making your own simplified form layouts.
+ */
+ function VisibleFields() {
+ return $this->fields->VisibleFields();
+ }
/**
* Setter for the form fields.
View
35 tests/forms/FieldListTest.php
@@ -729,4 +729,39 @@ function testMakeFieldReadonly() {
'Field nested inside a TabSet and FieldList can be marked readonly by FieldList->makeFieldReadonly()'
);
}
+
+ /**
+ * Test VisibleFields and HiddenFields
+ */
+ function testVisibleAndHiddenFields() {
+ $fields = new FieldList(
+ new TextField("A"),
+ new TextField("B"),
+ new HiddenField("C"),
+ new Tabset("Root",
+ new Tab("D",
+ new TextField("D1"),
+ new HiddenField("D2")
+ )
+ )
+ );
+
+ $hidden = $fields->HiddenFields();
+ // Inside hidden fields, all HiddenField objects are included, even nested ones
+ $this->assertNotNull($hidden->dataFieldByName('C'));
+ $this->assertNotNull($hidden->dataFieldByName('D2'));
+ // Visible fields are not
+ $this->assertNull($hidden->dataFieldByName('B'));
+ $this->assertNull($hidden->dataFieldByName('D1'));
+
+ $visible = $fields->VisibleFields();
+ // Visible fields exclude top level HiddenField objects
+ $this->assertNotNull($visible->dataFieldByName('A'));
+ $this->assertNull($visible->dataFieldByName('C'));
+ // But they don't exclude nested HiddenField objects. This is a limitation; you should
+ // put all your HiddenFields at the top level.
+ $this->assertNotNull($visible->dataFieldByName('D2'));
+ }
+
+
}
Please sign in to comment.
Something went wrong with that request. Please try again.