public function submit($data, Form $form) { $record = $this->getRecord(); $form->saveInto($record); $record->write(); return json_encode($record->toMap()); }
/** * Save handler * * @param array $data * @param Form $form * @return HTTPResponse */ public function save($data, $form) { $request = $this->getRequest(); $className = $this->stat('tree_class'); // Existing or new record? $id = $data['ID']; if (is_numeric($id) && $id > 0) { $record = DataObject::get_by_id($className, $id); if ($record && !$record->canEdit()) { return Security::permissionFailure($this); } if (!$record || !$record->ID) { $this->httpError(404, "Bad record ID #" . (int) $id); } } else { if (!singleton($this->stat('tree_class'))->canCreate()) { return Security::permissionFailure($this); } $record = $this->getNewItem($id, false); } // save form data into record $form->saveInto($record, true); $record->write(); $this->extend('onAfterSave', $record); $this->setCurrentPageID($record->ID); $message = _t('LeftAndMain.SAVEDUP', 'Saved.'); if ($request->getHeader('X-Formschema-Request')) { $schemaId = Controller::join_links($this->Link('schema/DetailEditForm'), $id); // Ensure that newly created records have all their data loaded back into the form. $form->loadDataFrom($record); $form->setMessage($message, 'good'); $data = $this->getSchemaForForm($form, $schemaId); $response = new HTTPResponse(Convert::raw2json($data)); $response->addHeader('Content-Type', 'application/json'); } else { $response = $this->getResponseNegotiator()->respond($request); } $response->addHeader('X-Status', rawurlencode($message)); return $response; }
/** * @param array $data * @param Form $form * @param HTTPRequest $request * @return DBHTMLText */ public function doEdit(array $data, Form $form, HTTPRequest $request) { // Check form field state if ($this->parent->isDisabled() || $this->parent->isReadonly()) { return $this->httpError(403); } // Check item permissions $item = $this->getItem(); if (!$item) { return $this->httpError(404); } if ($item instanceof Folder) { return $this->httpError(403); } if (!$item->canEdit()) { return $this->httpError(403); } $form->saveInto($item); $item->write(); $form->sessionMessage(_t('UploadField.Saved', 'Saved'), 'good'); return $this->edit($request); }
public function testLookupFieldDisabledSaving() { $object = new DataObjectTest_Team(); $form = new Form(new Controller(), 'Form', new FieldList(new LookupField('Players', 'Players')), new FieldList()); $form->loadDataFrom(array('Players' => array(14, 18, 22))); $form->saveInto($object); $playersIds = $object->Players()->getIDList(); $this->assertTrue($form->validate()); $this->assertEquals($playersIds, array(), 'saveInto() should not save into the DataObject for the LookupField'); }
/** * 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; }
/** * Update thisrecord * * @param array $data * @param Form $form * @param bool $doPublish * @return HTTPResponse */ protected function saveOrPublish($data, $form, $doPublish = false) { if (!isset($data['ID']) || !is_numeric($data['ID'])) { return (new HTTPResponse(json_encode(['status' => 'error']), 400))->addHeader('Content-Type', 'application/json'); } $id = (int) $data['ID']; /** @var File $record */ $record = $this->getList()->filter('ID', $id)->first(); if (!$record) { return (new HTTPResponse(json_encode(['status' => 'error']), 404))->addHeader('Content-Type', 'application/json'); } if (!$record->canEdit() || $doPublish && !$record->canPublish()) { return (new HTTPResponse(json_encode(['status' => 'error']), 401))->addHeader('Content-Type', 'application/json'); } $form->saveInto($record); $record->write(); // Publish this record and owned objects if ($doPublish) { $record->publishRecursive(); } // Note: Force return of schema / state in success result return $this->getRecordUpdatedResponse($record, $form); }