Skip to content

Commit 37eb638

Browse files
authored
Merge pull request #18155 from grokability/#18021-fix-patch-api-with-unique-serial
Override unique_undeleted in the form request
2 parents a9574e8 + 4ada47e commit 37eb638

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

app/Http/Requests/UpdateAssetRequest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,31 @@ public function authorize()
2828
*/
2929
public function rules()
3030
{
31+
$setting = Setting::getSettings();
32+
3133
$rules = array_merge(
3234
parent::rules(),
3335
(new Asset)->getRules(),
34-
// this is to overwrite rulesets that include required, and rewrite unique_undeleted
36+
// This overwrites the rulesets that are set at the model level (via Watson) but are not necessarily required at the request level when doing a PATCH update.
37+
// Confusingly, this skips the unique_undeleted validator at the model level (and therefore the UniqueUndeletedTrait), so we have to re-add those
38+
// rules here without the requiredness, since those values will already exist if you're updating an existing asset.
3539
[
3640
'model_id' => ['integer', 'exists:models,id,deleted_at,NULL', 'not_array'],
3741
'status_id' => ['integer', 'exists:status_labels,id'],
3842
'asset_tag' => [
3943
'min:1', 'max:255', 'not_array',
40-
Rule::unique('assets', 'asset_tag')->ignore($this->asset)->withoutTrashed()
44+
Rule::unique('assets', 'asset_tag')->ignore($this->asset)->withoutTrashed(),
45+
],
46+
'serial' => [
47+
'string', 'max:255', 'not_array',
48+
$setting->unique_serial=='1' ? Rule::unique('assets', 'serial')->ignore($this->asset)->withoutTrashed() : 'nullable',
4149
],
4250
],
4351
);
4452

4553
// if the purchase cost is passed in as a string **and** the digit_separator is ',' (as is common in the EU)
4654
// then we tweak the purchase_cost rule to make it a string
47-
if (Setting::getSettings()->digit_separator === '1.234,56' && is_string($this->input('purchase_cost'))) {
55+
if ($setting->digit_separator === '1.234,56' && is_string($this->input('purchase_cost'))) {
4856
$rules['purchase_cost'] = ['nullable', 'string'];
4957
}
5058

0 commit comments

Comments
 (0)