Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement field option to set as nullable for create input type #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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.
*
Expand Down
4 changes: 2 additions & 2 deletions src/Types/CreateInputType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should still be there. nullableOnCreate should just be an addition for more complex use cases where the default will be set by an observer.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, okay. 👍

return $fields->map(function (Field $field) use ($defaults) {
if ($field->isNullableOnCreate()) {
return $field->nullable();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Stubs/Schemas/UserSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down