blendbyte / nova-items-field

Laravel Nova array items field with sorting & validation
MIT License
12 stars 9 forks source link

Nova Items Field

Laravel Nova array items field with sorting, validation & many display options

nova-array-input-field

Installation

composer require blendbyte/nova-items-field

Usage

use NovaItemsField\Items;
function fields() {
    return [
        Items::make('Emails')
    ]
}

and be sure to cast the property as an array on your eloquent model

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

Validation

Use Laravel's built in array validation

Items::make('Emails')->rules([
     null => 'required|min:2'
     '*' => 'email|min:10',
]),

In this case, an error is produced if there aren't at least 2 items in the array and if each item is not a valid email or is shorter than 10 characters.

You might prefer to use explicit attribute names, the behaviour is exactly the same as before

Items::make('Emails', 'user_email')->rules([
    'user_email' => 'required|min:2',
    'user_email.*' => 'email|min:10',
]),

Array processing

Use the array to perform other actions by making an observer

function saving($user)
{
    foreach($user->emails as $email)
    {
        //
    }
}

Replace item vue component

Here's a brief walkthrough to customize the vue item - view

Additional options

function description default
->max(number) limit number of items allowed false
->draggable() turn on drag/drop sorting false
->fullWidth() increase the width of field area false
->maxHeight(pixel) limit the height of the list false
->listFirst() move add new to the bottom false
->inputType(text) text, date, etc "text"
->placeholder($value) the new item input text "Add a new item"
->deleteButtonValue($value) value for delete button "x"
->createButtonValue($value) value for create button "Add"
->hideCreateButton() hide the "add" button false
->indexAsList() display items as list on index instead of total number false
->detailsAsTotal() display total of items on detail view instead of list false