Skip to content

Commit 973282a

Browse files
authored
Ignore not found IDs (#39)
1 parent 0e21d25 commit 973282a

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/Traits/HasCustomFields.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,9 @@ public function order($fields)
6969
protected function validationData(array|null $fields, Collection $customFields): array
7070
{
7171
return collect($fields)
72-
->mapWithKeys(function (mixed $field, int $key) use ($customFields) {
73-
$id = $customFields->firstOrFail('id', $key)->id;
74-
75-
return ["field_{$id}" => $field];
76-
})->toArray();
72+
->intersectByKeys(array_flip($customFields->modelKeys()))
73+
->mapWithKeys(fn ($v, $k) => ["field_$k" => $v])
74+
->toArray();
7775
}
7876

7977
protected function validationRules(Collection $fields): array

tests/Feature/CustomFieldControllerTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public function can_overwrite_response_values()
9393
/** @test */
9494
public function invalid_data_throws_validation_exception()
9595
{
96+
/** @var Survey $survey */
9697
$survey = Survey::create();
9798
$survey->customfields()->save(
9899
CustomField::factory()->make([
@@ -119,7 +120,8 @@ public function invalid_data_throws_validation_exception()
119120
'custom_fields' => [
120121
$fieldId => 'Yeezus',
121122
],
122-
])->assertJsonFragment(["field_1" => ["The selected favorite_album is invalid."]]);
123+
])
124+
->assertJsonFragment(["field_1" => ["The selected favorite_album is invalid."]]);
123125
}
124126

125127
/** @test */

tests/Feature/HasCustomFieldsTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,22 @@ public function custom_fields_can_be_created_and_accessed_on_models_with_trait()
2727
$this->assertCount(1, $model->fresh()->customFields);
2828
$this->assertEquals('Lil Wayne', $model->fresh()->customFields->first()->description);
2929
}
30+
31+
/** @test */
32+
public function test_validating_unowned_custom_field_ids_are_ignored()
33+
{
34+
$model = Survey::create();
35+
36+
$customField = CustomField::factory()->make([
37+
'model_id' => $model->id,
38+
'model_type' => get_class($model),
39+
'type' => 'text',
40+
]);
41+
42+
$validator = $model->validateCustomFields([
43+
$customField->id + 1 => 'foo',
44+
]);
45+
46+
$this->assertTrue($validator->passes());
47+
}
3048
}

0 commit comments

Comments
 (0)