A field for creating and editing multiple records in a backend or frontend form.
- SilverStripe ^3.2
See License
private static $has_many = array('Cells', 'BasicContent');
$editor = MultiRecordField::create('ContentCellEditor', 'Content Cells', $this->Cells());
$fields->addFieldToTab('Root.ContentCells', $editor);
class Page extends SiteTree {
private static $has_many = array(
'Cells' => 'BasicContent',
);
public function getCMSFields()
{
$fields = parent::getCMSFields();
$editor = MultiRecordField::create('ContentCellEditor', 'Content Cells', $this->Cells());
$fields->addFieldToTab('Root.ContentCells', $editor);
return $fields;
}
}
class Page_Controller extends ContentController {}
class BasicContent extends DataObject
{
private static $db = array(
'Title' => 'Varchar(255)',
'Description' => 'Text',
'Content' => 'HTMLText',
);
private static $has_one = array(
'Parent' => 'Page',
);
private static $many_many = array(
'Images' => 'Image',
);
public function getCMSFields()
{
$fields = parent::getCMSFields();
$uploadField = UploadField::create('Images', 'Images', $this->Images());
$uploadField->setAllowedFileCategories('image');
$fields->replaceField('Images', $uploadField);
return $fields;
}
}
MultiRecordField Nesting
The MultiRecordField
supports nesting of other
MultiRecordField
s. When the field detects a MultiRecordField
in the set of fields to edit, that field is added as another nested toggle
field inside the parent set of fields for editing.
Custom fields
The MultiRecordField
uses the output of getCMSFields
when building
the fieldlist used for editing. To provide an alternate set of fields, define
a getMultiRecordFields
method that returns a FieldList
object.
Additionally, the MultiRecordField
calls the updateMultiEditFields
extension hook on the record being edited to allow extensions a chance to
change the fields.
Unit Tests
- Ensure all form data is restored correctly when a Form $Validator returns false
- Jake Bentvelzen (SilbinaryWolf) jake@silverstripe.com.au