/**
  * Loads the given form data into the underlying dataobject and relation
  *
  * @param array $data
  * @param Form $form
  * @throws ValidationException On error
  * @return DataObject Saved record
  */
 protected function saveFormIntoRecord($data, $form)
 {
     $list = $this->gridField->getList();
     // Check object matches the correct classname
     if (isset($data['ClassName']) && $data['ClassName'] != $this->record->ClassName) {
         $newClassName = $data['ClassName'];
         // The records originally saved attribute was overwritten by $form->saveInto($record) before.
         // This is necessary for newClassInstance() to work as expected, and trigger change detection
         // on the ClassName attribute
         $this->record->setClassName($this->record->ClassName);
         // Replace $record with a new instance
         $this->record = $this->record->newClassInstance($newClassName);
     }
     // Save form and any extra saved data into this dataobject
     $form->saveInto($this->record);
     $this->record->write();
     $extraData = $this->getExtraSavedData($this->record, $list);
     $list->add($this->record, $extraData);
     return $this->record;
 }