eom-plus / nova-depend-fill
Fill Laravel Nova field based on value from other field.
Fund package maintenance!
operativeit
Installs: 1 350
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: ^7.3|^8.0
- laravel/framework: ^8.0|^9.0
- laravel/nova: ^4.0
README
Installation
composer require eom-plus/nova-depend-fill
Requirements
php: >=8.0
laravel/nova: ^4.0
Description
Fill value and/or update options of a Laravel Nova field depends of value of another field.
This custom field is for Nova 4 only and it's based on the following original works:
Usage
You can fill one or multiple fields. Values are fetch from callback set to method updateValuesUsing(). You can also using magic methods such update[Field]ValueUsing() (eg: updateEmailValueUsing) with individual callback per target field.
You can set select options in the same way using global method setOptionsUsing() or magic one such set[Field]OptionsUsing() (eg: setPersonOptionsUsing).
Simple example
DependFill::make('template') ->fields([ Email::make('email'), Text::make('name'), ]) ->dependsOn( Select::make('person') ->options([ 'person1' => 'Person 1', 'person2' => 'Person 2' ]) ) ->updateValuesUsing(function ($value) { return [ 'name' => Str::upper($value), 'email' => $value '@mydomain.local', ]; }) ->withMeta(['value' => 'person2']) ])
Example with only one target field and magic callback method "updateEmailValueUsing"
DependFill::make('template') ->field( Email::make('email'), ) ->dependsOn( Select::make('person') ->options([ 'person1' => 'Person 1', 'person2' => 'Person 2' ]) ) ->updateEmailValueUsing(function ($value) { return $value . '@mydomain.com'; }) ->withMeta(['value' => 'person2']) ])
Example filling dynamically select list using a callback.
Options format that must return callback is not exactly the same as Nova documentation, you must specify 'value' key in the list instead use it as main array key
$options = [
['label' => 'Person 1', 'value' => 'person1', 'group'=> 'group 1'],
['label' => 'Person 2', 'value' => 'person2', 'group'=> 'group 1']
]
instead of
$options = [
'person1' => ['label' => 'Person 1' 'group'=> 'group 1'],
'person2' => ['label' => 'Person 2', 'group'=> 'group 1']
]
DependFill::make('template') ->field( Email::make('email'), ) ->dependsOn( Select::make('person') ) ->setPersonOptionsUsing(function($value, $model) { return [ ['label' => 'Person 1', 'value' => 'person1', 'group'=> 'group 1'], ['label' => 'Person 2', 'value' => 'person2', 'group'=> 'group 1'] ]; }) ->updateEmailValueUsing(function ($value) { return $value . '@mydomain.com'; }) ->withMeta(['value' => 'person2']) ])
Known bugs
Fields are not rendered properly on index and details page.
Future developments
- Add support for relevant (hide/show), readonly, disabled based on dependencies and mightbe on expression as in XForms
- Might make options array format to match exactly the same as Nova select field
- Support for flexible content
- ...