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

Checks that custom fields exist before saving #14565

Draft
wants to merge 5 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions app/Http/Requests/StoreAssetRequest.php
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Requests;

use App\Http\Requests\Traits\MayContainCustomFields;
use App\Models\Asset;
use App\Models\Company;
use Carbon\Carbon;
Expand All @@ -10,6 +11,7 @@

class StoreAssetRequest extends ImageUploadRequest
{
use MayContainCustomFields;
/**
* Determine if the user is authorized to make this request.
*
Expand Down
40 changes: 40 additions & 0 deletions app/Http/Requests/Traits/MayContainCustomFields.php
@@ -0,0 +1,40 @@
<?php

namespace App\Http\Requests\Traits;

use App\Models\AssetModel;
use App\Models\CustomField;

trait MayContainCustomFields
{
// this gets called automatically on a form request
public function withValidator($validator)
{
// find the model
if ($this->method() == 'POST') {
$asset_model = AssetModel::find($this->model_id);
}
if ($this->method() == 'PATCH' || $this->method() == 'PUT') {
// this is dependent on the asset update request PR
$asset_model = $this->asset;
}
// collect the custom fields in the request
$validator->after(function ($validator) use ($asset_model) {
$request_fields = $this->collect()->keys()->filter(function ($attributes) {
return str_starts_with($attributes, '_snipeit_');
});
// if there are custom fields, find the one's that don't exist on the model's fieldset and add an error to the validator's error bag
if (count($request_fields) > 0) {
$request_fields->diff($asset_model->fieldset->fields->pluck('db_column'))
->each(function ($request_field_name) use ($request_fields, $validator) {
if (CustomField::where('db_column', $request_field_name)->exists()) {
$validator->errors()->add($request_field_name, trans('validation.custom.custom_field_not_found_on_model'));
} else {
$validator->errors()->add($request_field_name, trans('validation.custom.custom_field_not_found'));
}
});
}
});
}
}

3 changes: 2 additions & 1 deletion resources/lang/en-US/validation.php
Expand Up @@ -126,6 +126,8 @@
'hashed_pass' => 'Your current password is incorrect',
'dumbpwd' => 'That password is too common.',
'statuslabel_type' => 'You must select a valid status label type',
'custom_field_not_found' => 'This field does not seem to exist, please double check your custom field names.',
'custom_field_not_found_on_model' => 'This field seems to exist, but is not available on this Asset Model\'s fieldset.',

// date_format validation with slightly less stupid messages. It duplicates a lot, but it gets the job done :(
// We use this because the default error message for date_format is reflects php Y-m-d, which non-PHP
Expand All @@ -137,7 +139,6 @@
'expected_checkin.date_format' => 'The :attribute must be a valid date in YYYY-MM-DD format',
'start_date.date_format' => 'The :attribute must be a valid date in YYYY-MM-DD format',
'end_date.date_format' => 'The :attribute must be a valid date in YYYY-MM-DD format',

],

/*
Expand Down