Skip to content

Commit

Permalink
progress, going to sleep
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerrlongg committed Feb 14, 2024
1 parent 57a75e6 commit 2524154
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
6 changes: 4 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 @@ -169,6 +170,7 @@ public function update(ImageUploadRequest $request, $modelId = null)

if ($this->shouldAddDefaultValues($request->input())) {
if (!$this->assignCustomFieldsDefaultValues($model, $request->input('default_values'))){
//TODO: this needs to return the actual validation errors, will come back to this before opening PR
return redirect()->back()->withInput()->with('error', trans('admin/custom_fields/message.fieldset_default_value.error'));
}
}
Expand Down Expand Up @@ -489,11 +491,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
2 changes: 1 addition & 1 deletion app/Models/CustomFieldset.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function validation_rules()
if ($field->element == 'checkbox') {
//Log::alert($field->formatFieldValuesAsArray());
$values = $field->formatFieldValuesAsArray();
$rules[$field->db_column_name()] = 'checkboxes';
$rules[$field->db_column_name()][] = 'checkboxes';
}
}

Expand Down
19 changes: 10 additions & 9 deletions app/Providers/ValidationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,23 +297,24 @@ public function boot()
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){
$options = CustomField::where('db_column', $attribute)->first()->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
if(!is_array($value)) {
elseif(!is_array($value)) {
$exploded = explode(',', $value);
$invalid = array_diff($exploded, $options);
if(count($invalid) > 0) {
return false;
}
} else {
$valid = array_intersect($value, $options);
if(array_count_values($valid) > 0) {
return true;
}
}


} else return true;
});
}

Expand Down

0 comments on commit 2524154

Please sign in to comment.