Skip to content

Commit 4d2e00d

Browse files
authored
Merge pull request #17 from OnrampLab/16-fix-not-require-field-cant-store-with-null-value
16 fix not require field cant store with null value
2 parents cd69631 + 7f01802 commit 4d2e00d

File tree

8 files changed

+41
-14
lines changed

8 files changed

+41
-14
lines changed

src/Concerns/Customizable.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ public function saveCustomFieldValues(): void
100100
$customFields->each(function ($customField) use ($validatedCustomFieldValues) {
101101
if (array_key_exists($customField->key, $validatedCustomFieldValues)) {
102102
$value = $validatedCustomFieldValues[$customField->key];
103+
if (is_null($value)) {
104+
return;
105+
}
103106
$constraints = [
104107
'custom_field_id' => $customField->id,
105108
'customizable_type' => $this->getMorphClass(),
@@ -117,6 +120,10 @@ public function saveCustomFieldValues(): void
117120
public function loadCustomFieldValues(): void
118121
{
119122
if ($this->customFieldValues->isEmpty()) {
123+
$this->getCustomFields()->each(function (CustomField $customField) {
124+
$attribute = "custom_{$customField->key}";
125+
$this->setAttribute($attribute, $customField->default_value);
126+
});
120127
return;
121128
}
122129

src/Models/BooleanCustomField.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ class BooleanCustomField extends CustomField
1010

1111
public function getValidationRule(): array
1212
{
13+
$rules = ['boolean'];
14+
$rules[] = $this->required ? 'required' : 'nullable';
1315
return [
14-
$this->key => ['boolean'],
16+
$this->key => $rules,
1517
];
1618
}
1719

1820
public function parseValue($value): bool
1921
{
2022
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
2123
}
22-
2324
}

src/Models/DateTimeCustomField.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ class DateTimeCustomField extends CustomField
1111

1212
public function getValidationRule(): array
1313
{
14+
$rules = ['date'];
15+
$rules[] = $this->required ? 'required' : 'nullable';
1416
return [
15-
$this->key => ['date'],
17+
$this->key => $rules,
1618
];
1719
}
1820
public function parseValue($value): string

src/Models/FloatCustomField.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ class FloatCustomField extends CustomField
1010

1111
public function getValidationRule(): array
1212
{
13+
$rules = ['numeric'];
14+
$rules[] = $this->required ? 'required' : 'nullable';
1315
return [
14-
$this->key => ['numeric'],
16+
$this->key => $rules,
1517
];
1618
}
1719

src/Models/IntegerCustomField.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ class IntegerCustomField extends CustomField
1010

1111
public function getValidationRule(): array
1212
{
13+
$rules = ['integer'];
14+
$rules[] = $this->required ? 'required' : 'nullable';
1315
return [
14-
$this->key => ['integer'],
16+
$this->key => $rules,
1517
];
1618
}
1719

src/Models/SelectCustomField.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ class SelectCustomField extends CustomField
1212
public function getValidationRule(): array
1313
{
1414
$options = $this->available_options->pluck('value')->toArray();
15+
$rules = [Rule::in($options)];
16+
$rules[] = $this->required ? 'required' : 'nullable';
1517
return [
16-
$this->key => [Rule::in($options)]
18+
$this->key => $rules,
1719
];
1820
}
1921
public function parseValue($value): string

src/Models/TextCustomField.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ class TextCustomField extends CustomField
1010

1111
public function getValidationRule(): array
1212
{
13+
$rules = ['string'];
14+
$rules[] = $this->required ? 'required' : 'nullable';
1315
return [
14-
$this->key => ['string'],
16+
$this->key => $rules,
1517
];
1618
}
1719

tests/Unit/Concerns/CustomizableTest.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ protected function setUp(): void
1818
$attributes = [
1919
'key' => 'zip_code',
2020
'type' => 'text',
21+
'required' => false,
2122
'model_class' => User::class,
2223
'contextable_id' => $this->account->id,
2324
'contextable_type' => $this->account->getMorphClass()
@@ -50,11 +51,13 @@ public function validate_custom_fields_should_work()
5051
* @test
5152
* @dataProvider customFieldDataProvider
5253
*/
53-
public function custom_load_custom_field_values_should_work($type, $value, $expected): void
54+
public function custom_load_custom_field_values_should_work($type, $value, $expected, $required, $defaultValue): void
5455
{
5556
$attributes = [
5657
'key' => 'field',
5758
'type' => $type,
59+
'required' => $required,
60+
'default_value' => $defaultValue,
5861
'model_class' => User::class,
5962
'contextable_id' => $this->account->id,
6063
'contextable_type' => $this->account->getMorphClass()
@@ -80,12 +83,18 @@ public function custom_load_custom_field_values_should_work($type, $value, $expe
8083
public function customFieldDataProvider(): array
8184
{
8285
return [
83-
'Text field' => ['text', 'Value', 'Value'],
84-
'Integer field' => ['integer', '42', 42],
85-
'Float field' => ['float', '3.14', 3.14],
86-
'Datetime field' => ['datetime', '2023-05-16 12:34:56', '2023-05-16 12:34:56'],
87-
'Select field' => ['select', 'Option 1', 'Option 1'],
88-
'Boolean field' => ['boolean', '1', true],
86+
'Text field' => ['text', 'Value', 'Value', true, ''],
87+
'Integer field' => ['integer', '42', 42, true, ''],
88+
'Float field' => ['float', '3.14', 3.14, true, ''],
89+
'Datetime field' => ['datetime', '2023-05-16 12:34:56', '2023-05-16 12:34:56', true, ''],
90+
'Select field' => ['select', 'Option 1', 'Option 1', true, ''],
91+
'Boolean field' => ['boolean', '1', true, true, ''],
92+
'Text field nullable' => ['text', null, 'Value', false, 'Value'],
93+
'Integer field nullable' => ['integer', null, 42, false, '42'],
94+
'Float field nullable' => ['float', null, 3.14, false, '3.14'],
95+
'Datetime field nullable' => ['datetime', null, '2023-05-16 12:34:56', false, '2023-05-16 12:34:56'],
96+
'Select field nullable' => ['select', null, 'Option 1', false, 'Option 1'],
97+
'Boolean field nullable' => ['boolean', null, true, false, true],
8998
];
9099
}
91100
}

0 commit comments

Comments
 (0)