Skip to content

16 fix not require field cant store with null value #17

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

Merged
Merged
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
7 changes: 7 additions & 0 deletions src/Concerns/Customizable.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ public function saveCustomFieldValues(): void
$customFields->each(function ($customField) use ($validatedCustomFieldValues) {
if (array_key_exists($customField->key, $validatedCustomFieldValues)) {
$value = $validatedCustomFieldValues[$customField->key];
if (is_null($value)) {
return;
}
$constraints = [
'custom_field_id' => $customField->id,
'customizable_type' => $this->getMorphClass(),
Expand All @@ -117,6 +120,10 @@ public function saveCustomFieldValues(): void
public function loadCustomFieldValues(): void
{
if ($this->customFieldValues->isEmpty()) {
$this->getCustomFields()->each(function (CustomField $customField) {
$attribute = "custom_{$customField->key}";
$this->setAttribute($attribute, $customField->default_value);
});
return;
}

Expand Down
5 changes: 3 additions & 2 deletions src/Models/BooleanCustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ class BooleanCustomField extends CustomField

public function getValidationRule(): array
{
$rules = ['boolean'];
$rules[] = $this->required ? 'required' : 'nullable';
return [
$this->key => ['boolean'],
$this->key => $rules,
];
}

public function parseValue($value): bool
{
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
}

}
4 changes: 3 additions & 1 deletion src/Models/DateTimeCustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ class DateTimeCustomField extends CustomField

public function getValidationRule(): array
{
$rules = ['date'];
$rules[] = $this->required ? 'required' : 'nullable';
return [
$this->key => ['date'],
$this->key => $rules,
];
}
public function parseValue($value): string
Expand Down
4 changes: 3 additions & 1 deletion src/Models/FloatCustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ class FloatCustomField extends CustomField

public function getValidationRule(): array
{
$rules = ['numeric'];
$rules[] = $this->required ? 'required' : 'nullable';
return [
$this->key => ['numeric'],
$this->key => $rules,
];
}

Expand Down
4 changes: 3 additions & 1 deletion src/Models/IntegerCustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ class IntegerCustomField extends CustomField

public function getValidationRule(): array
{
$rules = ['integer'];
$rules[] = $this->required ? 'required' : 'nullable';
return [
$this->key => ['integer'],
$this->key => $rules,
];
}

Expand Down
4 changes: 3 additions & 1 deletion src/Models/SelectCustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ class SelectCustomField extends CustomField
public function getValidationRule(): array
{
$options = $this->available_options->pluck('value')->toArray();
$rules = [Rule::in($options)];
$rules[] = $this->required ? 'required' : 'nullable';
return [
$this->key => [Rule::in($options)]
$this->key => $rules,
];
}
public function parseValue($value): string
Expand Down
4 changes: 3 additions & 1 deletion src/Models/TextCustomField.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ class TextCustomField extends CustomField

public function getValidationRule(): array
{
$rules = ['string'];
$rules[] = $this->required ? 'required' : 'nullable';
return [
$this->key => ['string'],
$this->key => $rules,
];
}

Expand Down
23 changes: 16 additions & 7 deletions tests/Unit/Concerns/CustomizableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ protected function setUp(): void
$attributes = [
'key' => 'zip_code',
'type' => 'text',
'required' => false,
'model_class' => User::class,
'contextable_id' => $this->account->id,
'contextable_type' => $this->account->getMorphClass()
Expand Down Expand Up @@ -50,11 +51,13 @@ public function validate_custom_fields_should_work()
* @test
* @dataProvider customFieldDataProvider
*/
public function custom_load_custom_field_values_should_work($type, $value, $expected): void
public function custom_load_custom_field_values_should_work($type, $value, $expected, $required, $defaultValue): void
{
$attributes = [
'key' => 'field',
'type' => $type,
'required' => $required,
'default_value' => $defaultValue,
'model_class' => User::class,
'contextable_id' => $this->account->id,
'contextable_type' => $this->account->getMorphClass()
Expand All @@ -80,12 +83,18 @@ public function custom_load_custom_field_values_should_work($type, $value, $expe
public function customFieldDataProvider(): array
{
return [
'Text field' => ['text', 'Value', 'Value'],
'Integer field' => ['integer', '42', 42],
'Float field' => ['float', '3.14', 3.14],
'Datetime field' => ['datetime', '2023-05-16 12:34:56', '2023-05-16 12:34:56'],
'Select field' => ['select', 'Option 1', 'Option 1'],
'Boolean field' => ['boolean', '1', true],
'Text field' => ['text', 'Value', 'Value', true, ''],
'Integer field' => ['integer', '42', 42, true, ''],
'Float field' => ['float', '3.14', 3.14, true, ''],
'Datetime field' => ['datetime', '2023-05-16 12:34:56', '2023-05-16 12:34:56', true, ''],
'Select field' => ['select', 'Option 1', 'Option 1', true, ''],
'Boolean field' => ['boolean', '1', true, true, ''],
'Text field nullable' => ['text', null, 'Value', false, 'Value'],
'Integer field nullable' => ['integer', null, 42, false, '42'],
'Float field nullable' => ['float', null, 3.14, false, '3.14'],
'Datetime field nullable' => ['datetime', null, '2023-05-16 12:34:56', false, '2023-05-16 12:34:56'],
'Select field nullable' => ['select', null, 'Option 1', false, 'Option 1'],
'Boolean field nullable' => ['boolean', null, true, false, true],
];
}
}