diff --git a/app/Models/CustomFieldset.php b/app/Models/CustomFieldset.php index e9be7c53c9..373c137bc4 100644 --- a/app/Models/CustomFieldset.php +++ b/app/Models/CustomFieldset.php @@ -5,6 +5,8 @@ namespace App\Models; 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 @@ -97,6 +99,11 @@ class CustomFieldset extends Model if ($field->element != 'checkbox') { $rules[$field->db_column_name()][] = 'not_array'; } + if ($field->element == 'checkbox') { + //Log::alert($field->formatFieldValuesAsArray()); + $values = $field->formatFieldValuesAsArray(); + //$rules[$field->db_column_name()] = 'checkboxes'; + } } return $rules; diff --git a/app/Providers/ValidationServiceProvider.php b/app/Providers/ValidationServiceProvider.php index 50468c8d72..754c2de9a0 100644 --- a/app/Providers/ValidationServiceProvider.php +++ b/app/Providers/ValidationServiceProvider.php @@ -2,9 +2,11 @@ namespace App\Providers; +use App\Models\CustomField; use App\Models\Department; use App\Models\Setting; use DB; +use Illuminate\Support\Facades\Log; use Illuminate\Support\ServiceProvider; use Illuminate\Validation\Rule; use Validator; @@ -294,6 +296,25 @@ class ValidationServiceProvider extends ServiceProvider Validator::extend('not_array', function ($attribute, $value, $parameters, $validator) { return !is_array($value); }); + + Validator::extend('checkboxes', function ($attribute, $value, $parameters, $validator){ + $options = CustomField::where('db_column', $attribute)->formatFieldValuesAsArray(); + if(!is_array($value)) { + $exploded = explode(',', $value); + $valid = array_intersect($exploded, $options); + if(array_count_values($valid) > 0) { + return true; + } + } + if(is_array($value)) { + $valid = array_intersect($value, $options); + if(array_count_values($valid) > 0) { + return true; + } + } + + + }); } /**