mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
API CHANGE: Added Form::VisibleFields() and FieldList::VisibleFields(), which list everything except hidden fields, to assist with the creation of custom form layouts.
This commit is contained in:
parent
ae45ef3bc2
commit
ba93028b01
@ -435,6 +435,20 @@ class FieldList extends ArrayList {
|
||||
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,
|
||||
* e.g. $this->transform(new ReadonlyTransformation())
|
||||
|
@ -544,6 +544,14 @@ class Form extends RequestHandler {
|
||||
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.
|
||||
*
|
||||
|
@ -729,4 +729,39 @@ class FieldListTest extends SapphireTest {
|
||||
'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'));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user