new validator for radio buttons

This commit is contained in:
spencerrlongg 2024-03-25 21:03:13 -05:00
parent 9b40c9788f
commit 5cf1a6c300
3 changed files with 13 additions and 0 deletions

View file

@ -103,6 +103,10 @@ class CustomFieldset extends Model
if ($field->element == 'checkbox') {
$rules[$field->db_column_name()][] = 'checkboxes';
}
if ($field->element == 'radio') {
$rules[$field->db_column_name()][] = 'radio_buttons';
}
}
return $rules;

View file

@ -322,6 +322,14 @@ class ValidationServiceProvider extends ServiceProvider
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);
});
}
/**

View file

@ -106,6 +106,7 @@ return [
'numeric' => 'Value cannot be negative'
],
'checkboxes' => ':attribute contains invalid options.',
'radio_buttons' => ':attribute is invalid.',
/*