record in Create Record is null

According to the docs, this should output the record before it gets created, however it shows up null.
class CreateTest extends CreateRecord
{
protected static string $resource = TestResource::class;

protected function beforeCreate(): void
{
$record = $this->getRecord();
dd($record);
}
}
class CreateTest extends CreateRecord
{
protected static string $resource = TestResource::class;

protected function beforeCreate(): void
{
$record = $this->getRecord();
dd($record);
}
}
Any idea why?
6 Replies
Matthew
Matthew6mo ago
Is this a mistake? How can you have a record before you create? anyone?
Tobias Platen
Tobias Platen6mo ago
Looks to me like a copy error in the documentation. In the EditRecord class, the getRecord() method should, as documented, return the corresponding model. However, in the CreateRecord class, the getRecord() method will only return the model in the last lifecycle hook afterCreate(). Before that, you will receive null. To access the data before that, you will need to use $this->form->getState(). Do you have an example case in which the $this->halt() method can be correctly explained?
Matthew
Matthew6mo ago
Yes I do!
protected function beforeCreate(): void
{
$inputData = $this->data;
if (Carbon::createFromFormat('Y-m-d H:i:s', $inputData['deadline'], config('app.timezone'))->isPast()){
$this->halt();
}
}
protected function beforeCreate(): void
{
$inputData = $this->data;
if (Carbon::createFromFormat('Y-m-d H:i:s', $inputData['deadline'], config('app.timezone'))->isPast()){
$this->halt();
}
}
krekas
krekas6mo ago
think about it. how can $this->getRecord() be not null if record isn't created yet? can you post the link to docs
Tobias Platen
Tobias Platen6mo ago
This is more for validation purposes. The $this->halt() method should already be demonstrated with an example that cannot be easily resolved through validation alone.
krekas
krekas6mo ago
For validation purpose? That's what validation is for