diff --git a/app/Http/Controllers/AssetModelsController.php b/app/Http/Controllers/AssetModelsController.php index 012f40e399..455fc82491 100755 --- a/app/Http/Controllers/AssetModelsController.php +++ b/app/Http/Controllers/AssetModelsController.php @@ -7,6 +7,7 @@ use App\Http\Requests\ImageUploadRequest; 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; @@ -169,6 +170,7 @@ class AssetModelsController extends Controller 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')); } } @@ -489,11 +491,11 @@ class AssetModelsController extends Controller * @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; } diff --git a/app/Models/CustomFieldset.php b/app/Models/CustomFieldset.php index c73d277a10..f5e9897dd3 100644 --- a/app/Models/CustomFieldset.php +++ b/app/Models/CustomFieldset.php @@ -102,7 +102,7 @@ class CustomFieldset extends Model if ($field->element == 'checkbox') { //Log::alert($field->formatFieldValuesAsArray()); $values = $field->formatFieldValuesAsArray(); - $rules[$field->db_column_name()] = 'checkboxes'; + $rules[$field->db_column_name()][] = 'checkboxes'; } } diff --git a/app/Providers/ValidationServiceProvider.php b/app/Providers/ValidationServiceProvider.php index 860564ec1f..0bd201a262 100644 --- a/app/Providers/ValidationServiceProvider.php +++ b/app/Providers/ValidationServiceProvider.php @@ -297,23 +297,24 @@ class ValidationServiceProvider extends ServiceProvider 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; }); }