Skip to content

Commit b3d26f4

Browse files
committed
Implement field option to set as nullable for create input type
Fixes #78
1 parent 4c71e53 commit b3d26f4

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

src/Fields/Field.php

+30
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ class Field
4646
*/
4747
protected $nullable = false;
4848

49+
/**
50+
* Indicates if the field can be omitted on create.
51+
*
52+
* @var bool
53+
*/
54+
protected $nullableOnCreate = false;
55+
4956
/**
5057
* @var bool
5158
*/
@@ -276,6 +283,29 @@ public function isNullable(): bool
276283
return $this->nullable;
277284
}
278285

286+
/**
287+
* Set if the field can be omitted on create.
288+
*
289+
* @param bool $nullable
290+
* @return \Bakery\Fields\Field
291+
*/
292+
public function nullableOnCreate(bool $nullable = true): self
293+
{
294+
$this->nullableOnCreate = $nullable;
295+
296+
return $this;
297+
}
298+
299+
/**
300+
* Determine if the field can be omitted on create.
301+
*
302+
* @return bool
303+
*/
304+
public function isNullableOnCreate()
305+
{
306+
return $this->nullableOnCreate;
307+
}
308+
279309
/**
280310
* Set if the field has nullable items.
281311
*

src/Types/CreateInputType.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ protected function getFillableFields(): Collection
4949
$fields = parent::getFillableFields();
5050
$defaults = $this->model->getAttributes();
5151

52-
return $fields->map(function (Field $field, $key) use ($defaults) {
53-
if (in_array($key, array_keys($defaults))) {
52+
return $fields->map(function (Field $field) use ($defaults) {
53+
if ($field->isNullableOnCreate()) {
5454
return $field->nullable();
5555
}
5656

tests/Stubs/Schemas/UserSchema.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function fields(): array
1616
return [
1717
'name' => Field::string()->searchable(),
1818
'email' => Field::string()->unique()->searchable(),
19-
'type' => Field::string()->canStoreWhen('setType'),
19+
'type' => Field::string()->nullableOnCreate()->canStoreWhen('setType'),
2020
'password' => Field::string()->canSeeWhen('readPassword'),
2121
'secret_information' => Field::string()
2222
->canSee(function (?Authenticatable $user, User $source) {

0 commit comments

Comments
 (0)