diff --git a/src/Fields/Field.php b/src/Fields/Field.php index d1af8621..51aa2d39 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -46,6 +46,13 @@ class Field */ protected $nullable = false; + /** + * Indicates if the field can be omitted on create. + * + * @var bool + */ + protected $nullableOnCreate = false; + /** * @var bool */ @@ -276,6 +283,29 @@ public function isNullable(): bool return $this->nullable; } + /** + * Set if the field can be omitted on create. + * + * @param bool $nullable + * @return \Bakery\Fields\Field + */ + public function nullableOnCreate(bool $nullable = true): self + { + $this->nullableOnCreate = $nullable; + + return $this; + } + + /** + * Determine if the field can be omitted on create. + * + * @return bool + */ + public function isNullableOnCreate() + { + return $this->nullableOnCreate; + } + /** * Set if the field has nullable items. * diff --git a/src/Types/CreateInputType.php b/src/Types/CreateInputType.php index 01e589c3..c50f9f81 100644 --- a/src/Types/CreateInputType.php +++ b/src/Types/CreateInputType.php @@ -49,8 +49,8 @@ protected function getFillableFields(): Collection $fields = parent::getFillableFields(); $defaults = $this->model->getAttributes(); - return $fields->map(function (Field $field, $key) use ($defaults) { - if (in_array($key, array_keys($defaults))) { + return $fields->map(function (Field $field) use ($defaults) { + if ($field->isNullableOnCreate()) { return $field->nullable(); } diff --git a/tests/Stubs/Schemas/UserSchema.php b/tests/Stubs/Schemas/UserSchema.php index 2614f473..8bd9ce7d 100644 --- a/tests/Stubs/Schemas/UserSchema.php +++ b/tests/Stubs/Schemas/UserSchema.php @@ -16,7 +16,7 @@ public function fields(): array return [ 'name' => Field::string()->searchable(), 'email' => Field::string()->unique()->searchable(), - 'type' => Field::string()->canStoreWhen('setType'), + 'type' => Field::string()->nullableOnCreate()->canStoreWhen('setType'), 'password' => Field::string()->canSeeWhen('readPassword'), 'secret_information' => Field::string() ->canSee(function (?Authenticatable $user, User $source) {