Skip to content

Laravel Nova array items field with sorting & validation

License

Notifications You must be signed in to change notification settings

dillingham/nova-items-field

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nova Items Field

Latest Version on Github Total Downloads

Simple Nova field for flat array inputs

form

Installation

composer require dillingham/nova-items-field

Usage

use NovaItemsField\Items;
Items::make('Emails')->values($this->emails),

Methods

function description required default
values(array) add a model's array Yes --
inputType(text) text, date, etc No "text"
fullWidth(boolean) increase size of field No false
placeholder(text) the new item input text No "Add a new item"
listFirst() move form after the list No false
deleteButtonValue(html) value for delete button No "x"
createButtonValue(html) value for create button No "Add"

Validation

Items::make('Emails')->values($this->emails)->rules([
    'emails.*' => 'email|min:10',
]),

validation

Sidenotes

Be sure to cast emails to an array in your eloquent model.

public $casts = [
    'emails' => 'array'
];

Don't want to store the array?

Use the array as a temporary variable

  • make an observer
  • handle the array manually.
  • unset the array to avoid sql error
function saving($user)
{
    foreach($user->emails as $email)
    {
        //
    }
    
    unset($user->emails);
}

Feature Todo

  • Laravel array validation
  • Sortable / draggable items