progress, going to sleep

This commit is contained in:
spencerrlongg 2024-02-14 02:12:31 -06:00
parent 57a75e68b9
commit 25241542d2
3 changed files with 15 additions and 12 deletions

View file

@ -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;
}

View file

@ -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';
}
}

View file

@ -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;
});
}