code16/sharp
Code-driven CMS framework for Laravel (PHP 8.3+/Laravel 11+). Build admin/CMS sections with a clean UI and strong DX: CRUD with validation, search/sort/filter, bulk or custom commands, and authorization—no front-end code required, data-agnostic.
Class: Code16\Sharp\Form\Fields\SharpFormSelectField
self::make(string $key, array $options)The $options array can be either:
id and label keys. Fore instance:[
['id'=>1, 'label'=>'Label 1'],
['id'=>2, 'label'=>'Label 2'],
]
This allows to write code like this:
SharpFormSelectField::make(
'travel_id',
Travel::orderBy('departure_date')
->get()
->map(function ($travel) {
return [
'id' => $travel->id,
'label' => $travel->departure_date->format('Y-m-d')
. ' — ' . $travel->destination
];
})
->all()
);
setMultiple(bool $multiple = true)Allow multi-selection (default: false)
setClearable(bool $clearable = true)Allow null value in non-multiple selection (default: false)
setDisplayAsList()Display as a list (the default value):
setDisplayAsDropdown()Display as a classic dropdown.
setMaxSelected(int $maxSelected)Set a maximum item selection (multiple only). Default: unlimited.
setMaxSelectedUnlimited()Unset a maximum item selection (multiple only).
setInline(bool $inline = true)Display an inline checklist (if multiple + display=list).
setIdAttribute(string $idAttribute)Set the id name attribute of options (default: "id").
setOptionsLinkedTo(string ...$fieldKeys)Thanks to this feature, you can link the dataset (meaning: the options) of the select to another field of the form (or even: to some other fields). In this case, the options array must be indexed with the value of the linked field.
For instance:
SharpFormSelectField::make(
'brand',
[
'France' => [
['id'=>1, 'label'=>'Renault'],
['id'=>2, 'label'=>'Peugeot'],
],
'Germany' => [
['id'=>3, 'label'=>'Audi'],
['id'=>4, 'label'=>'Mercedes'],
]
]
)->setOptionsLinkedTo('country')
This would work on relation with a country form field, which may be valued with "France" or "Germany".
In some cases you may want to depend on more than one field; you must add a nested level in the options array:
SharpFormSelectField::make(
'model',
[
'France' => [
1 => [['id'=>67, 'label'=>'Clio'], ...],
2 => ...
],
'Germany' => [
3 => [['id'=>98, 'label'=>'A4'], ...],
4 => ...
]
]
)->setOptionsLinkedTo('country', 'brand')
toFront: expects
fromFront: returns
[
['id' => 1],
['id' => 2]
]
How can I help you explore Laravel packages today?