Skip to content

Commit

Permalink
new validator for radio buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
spencerrlongg committed Mar 26, 2024
1 parent 9b40c97 commit 5cf1a6c
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions app/Models/CustomFieldset.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ public function validation_rules()
if ($field->element == 'checkbox') {
$rules[$field->db_column_name()][] = 'checkboxes';
}

if ($field->element == 'radio') {
$rules[$field->db_column_name()][] = 'radio_buttons';
}
}

return $rules;
Expand Down
8 changes: 8 additions & 0 deletions app/Providers/ValidationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,14 @@ public function boot()

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

/**
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en-US/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
'numeric' => 'Value cannot be negative'
],
'checkboxes' => ':attribute contains invalid options.',
'radio_buttons' => ':attribute is invalid.',


/*
Expand Down

0 comments on commit 5cf1a6c

Please sign in to comment.