The ID of the new, until now not created record.
Even if you did get the "ID" as @schellmax suggested you'd never be sure that the same "ID" can be used when saving. This if quite fragile and can be broken if e.g. multiple users try to do the same thing approximately at the same time.
Here's what I did for a project that needed to upload and attach files to a record before submitting the form.
- Save the record in the beginning regardless of wether it'll be submitted or not. Now you have the official
ID
for that record.
- Attach any files / other DataObjects as you would when you already have the record.
- When submitting the form optionally clean up the table in a way that doesn't destroy other in-use records.
This way you have the correct ID
no matter what happens and how many users try this at the same time. Also the performance hit should be minimal unless you have hundreds or thousands of users doing this at concurrently.
Example (not sure it works as-is as I stripped it down):
public function QuoteForm() {
$fields = new FieldList();
// Clears out quotes older than 7 days
Quote::removeEmptyQuotes();
// Get the quote.
$quote = $this->getQuote();
$yourName = new TextField(
'Name',
_t('QuoteForm.YourName', 'Your name')
);
$yourName->setAttribute('required', true);
$fields->push($yourName);
$uploadField = new UploadField(
'Files',
_t('QuoteForm.Files', 'Select file*'),
$quote->Files()
);
$uploadField->setRecord($quote);
$uploadField->setFolderName('quotefiles');
$uploadField->setConfig('canAttachExisting', false);
$uploadField->getValidator()->setAllowedExtensions(array(
'jpg', 'jpeg', 'png', 'gif', 'tiff',
'odt', 'pdf', 'rtf', 'txt',
'doc', 'docx',
'ppt', 'pptx',
'xls', 'xlsx'
));
$uploadField->setAttribute('required', true);
$fields->push($uploadField);
$actions = new FieldList(
new FormAction('saveQuoteRequest', _t('QuoteForm.Send', 'Send'))
);
return new Form($this, 'QuoteForm', $fields, $actions);
}
/*
* Selects quote based on possible QuoteID from request.
* If none are found / if the request param is empty,
* creates a new Quote.
*
* @return Quote
*/
protected function getQuote() {
$quote = null;
$quoteID = (int)Session::get('QuoteID');
if ($quoteID > 0) {
$quote = Quote::get()->byID($quoteID);
}
if ($quote === null) {
$quote = new Quote();
$quote->write();
}
Session::set('QuoteID', $quote->ID);
return $quote;
}