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

Add new validator for custom field checkboxes and fix asset model default updates #14369

Merged
merged 25 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
43d8474
a note to remember this tomorrow
spencerrlongg Feb 6, 2024
bcfa913
condition makes this work, needs more testing
spencerrlongg Feb 8, 2024
d553586
cleanup for pr
spencerrlongg Feb 13, 2024
dcf2168
initial stuff, need to switch branches
spencerrlongg Feb 14, 2024
57a75e6
maybe i do the inverse here?
spencerrlongg Feb 14, 2024
2524154
progress, going to sleep
spencerrlongg Feb 14, 2024
72c118a
cleanup
spencerrlongg Feb 14, 2024
d9c61fd
validation msg
spencerrlongg Feb 14, 2024
fb28882
trim potential spaces
spencerrlongg Feb 14, 2024
1ceb703
rm var
spencerrlongg Feb 14, 2024
115e0fc
implode submitted arrays to save
spencerrlongg Feb 14, 2024
c6d85a1
allows arrays on checkbox values
spencerrlongg Feb 20, 2024
26728a8
this seems to work for patches
spencerrlongg Feb 20, 2024
d67ff54
temporary decrypt, almost there
spencerrlongg Feb 20, 2024
20dbacd
store good, update needs work
spencerrlongg Feb 22, 2024
1435865
pushing to test other branches
spencerrlongg Feb 22, 2024
b6fa6cb
note before switching tasks
spencerrlongg Feb 22, 2024
ad0f873
rm validation stuff
spencerrlongg Mar 5, 2024
af06b1c
hide encryption option for checkbox and radio
spencerrlongg Mar 5, 2024
a251e61
rm commented code and add comment
spencerrlongg Mar 5, 2024
3a0a13d
tests written but something not working...
spencerrlongg Mar 5, 2024
04e0a9d
commented tests for now
spencerrlongg Mar 8, 2024
7e4a0ee
rm dumb note
spencerrlongg Mar 25, 2024
9b40c97
rm commented tests
spencerrlongg Mar 25, 2024
5cf1a6c
new validator for radio buttons
spencerrlongg Mar 26, 2024
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
20 changes: 17 additions & 3 deletions app/Http/Controllers/Api/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,11 @@ public function store(StoreAssetRequest $request): JsonResponse
}
}
}
if ($field->element == 'checkbox') {
if(is_array($field_val)) {
$field_val = implode(',', $field_val);
}
}


$asset->{$field->db_column} = $field_val;
Expand Down Expand Up @@ -652,13 +657,22 @@ public function update(ImageUploadRequest $request, $id)
// Update custom fields
if (($model) && (isset($model->fieldset))) {
foreach ($model->fieldset->fields as $field) {
$field_val = $request->input($field->db_column, null);

if ($request->has($field->db_column)) {
if ($field->field_encrypted == '1') {
if (Gate::allows('admin')) {
$asset->{$field->db_column} = \Crypt::encrypt($request->input($field->db_column));
$asset->{$field->db_column} = Crypt::encrypt($field_val);
}
} else {
$asset->{$field->db_column} = $request->input($field->db_column);
}
if ($field->element == 'checkbox') {
if(is_array($field_val)) {
$field_val = implode(',', $field_val);
$asset->{$field->db_column} = $field_val;
}
}
else {
$asset->{$field->db_column} = $field_val;
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions app/Http/Controllers/AssetModelsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Models\Actionlog;
use App\Models\Asset;
use App\Models\AssetModel;
use App\Models\CustomField;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
Expand Down Expand Up @@ -489,11 +490,11 @@ private function shouldAddDefaultValues(array $input)
* @param array $defaultValues
* @return void
*/
private function assignCustomFieldsDefaultValues(AssetModel $model, array $defaultValues)
private function assignCustomFieldsDefaultValues(AssetModel $model, array $defaultValues): bool
{
$data = array();
foreach ($defaultValues as $customFieldId => $defaultValue) {
$customField = \App\Models\CustomField::find($customFieldId);
$customField = CustomField::find($customFieldId);

$data[$customField->db_column] = $defaultValue;
}
Expand Down
17 changes: 15 additions & 2 deletions app/Models/CustomFieldset.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Gate;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\Rule;
use Watson\Validating\ValidatingTrait;

class CustomFieldset extends Model
Expand Down Expand Up @@ -92,8 +94,19 @@ public function validation_rules()

array_push($rule, $field->attributes['format']);
$rules[$field->db_column_name()] = $rule;
//add not_array to rules for all fields
$rules[$field->db_column_name()][] = 'not_array';

// add not_array to rules for all fields but checkboxes
if ($field->element != 'checkbox') {
$rules[$field->db_column_name()][] = 'not_array';
}

if ($field->element == 'checkbox') {
$rules[$field->db_column_name()][] = 'checkboxes';
}

if ($field->element == 'radio') {
$rules[$field->db_column_name()][] = 'radio_buttons';
}
}

return $rules;
Expand Down
36 changes: 36 additions & 0 deletions app/Providers/ValidationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace App\Providers;

use App\Models\CustomField;
use App\Models\Department;
use App\Models\Setting;
use DB;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\ServiceProvider;
use Illuminate\Validation\Rule;
use Validator;
Expand Down Expand Up @@ -294,6 +297,39 @@ public function boot()
Validator::extend('not_array', function ($attribute, $value, $parameters, $validator) {
return !is_array($value);
});

// This is only used in Models/CustomFieldset.php - it does automatic validation for checkboxes by making sure
// that the submitted values actually exist in the options.
Validator::extend('checkboxes', function ($attribute, $value, $parameters, $validator){
$field = CustomField::where('db_column', $attribute)->first();
$options = $field->formatFieldValuesAsArray();

if(is_array($value)) {
$invalid = array_diff($value, $options);
if(count($invalid) > 0) {
return false;
}
}

// for legacy, allows users to submit a comma separated string of options
elseif(!is_array($value)) {
$exploded = array_map('trim', explode(',', $value));
$invalid = array_diff($exploded, $options);
if(count($invalid) > 0) {
return false;
}
}

return true;
});

// Validates that a radio button option exists
Validator::extend('radio_buttons', function ($attribute, $value) {
$field = CustomField::where('db_column', $attribute)->first();
$options = $field->formatFieldValuesAsArray();

return in_array($value, $options);
});
}

/**
Expand Down
2 changes: 2 additions & 0 deletions resources/lang/en-US/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@
'gte' => [
'numeric' => 'Value cannot be negative'
],
'checkboxes' => ':attribute contains invalid options.',
'radio_buttons' => ':attribute is invalid.',


/*
Expand Down
8 changes: 6 additions & 2 deletions resources/views/custom_fields/fields/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@

@if (!$field->id)
<!-- Encrypted -->
<div class="col-md-9 col-md-offset-3">
<div class="col-md-9 col-md-offset-3" id="encryption_section">
<label class="form-control">
<input type="checkbox" value="1" name="field_encrypted" id="field_encrypted"{{ (Request::old('field_encrypted') || $field->field_encrypted) ? ' checked="checked"' : '' }}>
{{ trans('admin/custom_fields/general.encrypt_field') }}
Expand All @@ -146,7 +146,6 @@
<p><i class="fas fa-exclamation-triangle" aria-hidden="true"></i> {{ trans('admin/custom_fields/general.encrypt_field_help') }}</p>
</div>
</div>

@endif


Expand Down Expand Up @@ -298,11 +297,16 @@ class="fieldset"
}).change();

// Only display the field element if the type is not text
// and don't display encryption option for checkbox or radio
$(".field_element").change(function(){
$(this).find("option:selected").each(function(){
if (($(this).attr("value")!="text") && ($(this).attr("value")!="textarea")){
$("#field_values_text").show();
if ($(this).attr("value") == "checkbox" || $(this).attr("value") == "radio") {
$("#encryption_section").hide();
}
} else{
$("#encryption_section").show();
$("#field_values_text").hide();
}
});
Expand Down