Download the PHP package schenke-io/livewire-auto-form without Composer

On this page you can find all versions of the php package schenke-io/livewire-auto-form. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package livewire-auto-form

Version Downloads Tests License PHP

Livewire Auto Form

Enhanced livewire component to edit models and its relationships

Stop manually mapping every Eloquent attribute to a Livewire property and start focusing on your app's core logic with our buffer-based form management.

If you struggle with the following problems, this package is for you:

Core Concepts

Livewire Auto Form follows a buffer-based state management pattern. Instead of binding Livewire properties directly to Eloquent model attributes, it uses an internal $form object (an instance of FormCollection) to safely stage changes.

Core Principles

This approach ensures that your components remain clean, predictable, and easy to test.

Rule Inheritance & Value Normalization

AutoForm can inherit validation rules from your Eloquent model and normalize complex value objects in a predictable way.

Installation

Usage Patterns

Editing Nested JSON Attributes

Livewire Auto Form seamlessly supports Laravel's JSON-casted attributes. When you have a model with a JSON column, you can use dot-notation in ruleKeys() to include specific nested keys in your form buffer.

Model Setup

Ensure your model has the attribute cast to an array or object.

Component Setup

Use ruleKeys() to declare the nested JSON keys you want to edit. The package will automatically hydrate the form buffer with the nested values and handle the dotted state.

Blade View

Bind your inputs to the nested keys in the $form object.

The package's "Single Buffer" architecture ensures that when you save, the entire JSON structure is correctly merged and persisted back to the model attribute.

Code Examples

This guide provides examples for using the package, ranging from basic forms to more advanced scenarios.

Note on Method Calls: Since the form logic is integrated directly into the component, you can call methods like save() or edit() directly in Blade. This ensures full compatibility with Alpine.js and component libraries like Flux without the need for wrapper methods. See API Definitions for details.

1. The Basic Form (Manual Save)

Using scanInheritedRules with ruleKeys() to avoid duplication

You can let your models define rules() and inherit them in your component to keep validation logic in one place. Use ruleKeys() to declare which fields to inherit and pass overrides to scanInheritedRules() when needed.

This is the simplest way to use the package. You extend AutoForm in your component and initialize it.

The Livewire Component:

The Blade View:


2. Modern "Auto-Save" Experience

If you want your form to save automatically as the user types (on blur), just set $this->autoSave to true.

The Livewire Component:

The Blade View:


3. Handling Relationships

This is where the package really shines. Imagine a Brand that has many Products. You can edit the brand and its products in the same component.

The Livewire Component:

The Blade View:


4. Using Enums for Selects

The primary way to handle selection elements (selects, radios, etc.) is by implementing the AutoFormOptions interface on your Model or Enum.

The Enum:

The Blade View:

Automatic Fallback: If you don't implement AutoFormOptions, the package will automatically generate readable labels from your Enum case names or Model attributes!


5. Listening for Events (Notifications)

You can listen for the events dispatched by the component to show "Saved" notifications or other UI feedback.

The Blade View (using Alpine.js):


6. List & Edit Pattern

You can use a single component to manage a collection of models, allowing you to select and edit any record from a list, or create a new one, all within the same view state.

The Livewire Component:

The Blade View:


7. Multi-Step Wizard

For complex forms, extend AutoWizardForm to break them into steps. It handles step navigation, explicit field mapping, and per-step validation.

The Livewire Component:

The Main Blade View:


8. Redirecting after Save

If you need to perform actions after a successful save, such as redirecting the user to another page, you can easily override the save() method in your component.

The Livewire Component:

Multi-Step Wizards

The AutoWizardForm extends the core AutoForm functionality to support complex, multi-step workflows with ease. It handles step navigation, per-step validation, and a unified submission flow.

Magic Features

Configuration

To create a wizard, extend AutoWizardForm and configure the following:

API Reference

Method Description
submit() Handles both transitions and final saving. Calls next() or save().
next() Validates current step's fields and moves forward.
previous() Moves to the previous step.
isLastStep() Returns true if on the final step.
getSteps() Returns the list of defined step views.
isStepActive(int $index) Checks if a step is currently active.

Full Example

1. The Livewire Component:

2. The Main Blade View (user-wizard.blade.php):

3. Individual Step View (profile.blade.php):

API Definitions

The package provides two main classes for managing form state and persistence: AutoForm and AutoWizardForm. These classes are Base Components, offering a context-aware "Single Buffer" architecture.

Using AutoForm

To use the package, extend AutoForm in your Livewire component and initialize it in the mount() method:

Using AutoWizardForm

For multi-step workflows, extend AutoWizardForm. It provides step management and per-step validation.

See the Multi-Step Wizards guide for details.

Public Properties (AutoWizardForm)

Property Type Description
$currentStepIndex int The zero-based index of the current active step.
$structure array Map of Blade view names to field names.
$stepViewPrefix string Prefix for the step Blade views.

Public Methods (AutoWizardForm)

Method Description
submit() Handles navigation (next step) or final submission (if on last step).
next() Validates current step and moves forward.
previous() Moves to the previous step.
isLastStep() Returns true if on the final step.
getSteps() Returns the list of defined step views.
isStepActive(int $index) Checks if a step is currently active.
getStepFields(int $index) Returns the fields associated with a specific step.

Public Properties (AutoForm)

Property Type Description
$autoSave bool Default false. If true, fields are saved on every update (on blur). If false, you must call save() manually.
$form FormCollection The internal state container (read-only from outside).

The $form object (FormCollection)

The $form object contains the following state properties:

Property Type Description
activeContext string Current editing context: '' for root model or a relation name.
activeId int|string|null ID of the record being edited. null indicates "Add Mode".
rootModelClass string The class name of the main model.
rootModelId int|string|null The ID of the main model instance.
autoSave bool Whether auto-save is currently enabled.

View Actions (Public Interface)

edit(string $relation, int|string $id)

Switches the context to edit a record.

add(string $relation)

Switches the context to "Add Mode".

optionsFor(string $key, ?string $labelMask = null)

Generates option arrays for selects based on Enums or Eloquent relations.

scanInheritedRules(array $rules = [])

Automatically scans the model and its relations for validation rules defined in rules() or casts().

save()

Validates and persists the current buffer data.

cancel()

Resets context to root ('') and reloads data to discard changes.

delete(string $relation, int|string $id)

Deletes or detaches a record and updates the active context.

Calling Methods from Blade

Since the form logic is now part of the Component itself, you can call methods directly in Blade without any special wrappers:

This is fully compatible with Alpine.js and component libraries like Flux.

Helper Methods

getRelationList(string $relation)

Returns a collection of related models for the given relation name.

isEdited(string $relation, int|string $id)

Returns true if the specified related record is currently being edited.

getModel()

Returns the root model instance with current buffer data applied.

getActiveModel()

Returns the model instance for the current active context (root or relation) with current buffer data applied.

ruleKeys()

Defines which model fields are included in the form and should have their validation rules inherited. Returns an array of strings (e.g., ['name', 'posts.title']).

scanInheritedRules(array $rules = [])

Scans for rules from the active model and its relationships. This is used by the default rules() implementation to avoid duplicating validation rules already defined in your Eloquent models.

Returns the merged rules array.

Validation Flexibility: If a key in ruleKeys() cannot be resolved on the active model or if a relationship path is invalid, it defaults to nullable instead of throwing an exception. This allows for quick prototyping and optional fields.

optionsFor(string $key, ?string $labelMask = null)

Universal helper for fetching [value => label] pairs for selection elements (selects, radios, checkboxes).

Usage in Blade:

Primary Usage (AutoFormOptions Interface)

The recommended way to provide options is by implementing the AutoFormOptions interface on your Model or BackedEnum. This centralizes the logic and supports custom label formatting via the $labelMask.

Automatic Fallback

If the target class does not implement AutoFormOptions, the system uses an automatic generation strategy:

Labels generated via the fallback are automatically localized using Laravel's __().

Localization

The package supports both JSON and PHP-based localization storage. All labels generated for select options, whether via AutoFormOptions or the automatic fallback, are passed through Laravel's __() helper.

JSON-based Localization

Useful for full-sentence labels.

PHP-based Localization

Useful for structured keys.

Advanced Configuration (Replacements)

You can provide translation replacements by returning an array from getOptions():

The AutoFormLocalisedEnumOptions Trait

For Enums, you can use the AutoFormLocalisedEnumOptions trait to automate key generation:

This automatically resolves to the translation key enums.user_status.active. You can also override the prefix dynamically by passing it as the $labelMask to optionsFor('status', 'custom.prefix').

Exceptions

The package throws SchenkeIo\LivewireAutoForm\Helpers\LivewireAutoFormException for:

Persistence & Relationship Discovery

Livewire Auto Form features a hardened persistence layer that ensures data is correctly routed between your form buffer and the database, with a specific focus on Eloquent relationships.

Strict Relationship Validation

The package distinguishes between actual Eloquent relationships and other dot-notated data (such as JSON casts or nested arrays) by using the model's isRelation() method.

When you define dot-notated rules like:

The package performs the following validations:

  1. Rule Discovery: During initialization and rule inheritance, only keys that correspond to verified Eloquent relations are treated as relationship contexts.
  2. Persistence Protection: When saving data, the package verifies that a path segment is an actual relationship before attempting to invoke it as a method on the model.

If a dot-notated key does not correspond to a relationship, it is treated as a standard model attribute, allowing it to work seamlessly with Laravel's built-in support for JSON casts.

Relationship Hardening

The CrudProcessor has been hardened to prevent common errors when dealing with complex relationship trees:

Event System

Livewire Auto Form dispatches several browser events that you can listen for in Alpine.js or Livewire to provide real-time feedback to your users.

Available Events

1. saved

Dispatched after the entire form (or the active relationship context) has been successfully persisted to the database.

Parameters:

Example (Alpine.js):


2. field-updated

Dispatched when an individual field is automatically saved to the database (only when autoSave is enabled).

Parameters:

Example (Alpine.js):


3. confirm-discard-changes

Dispatched when a user action might result in losing unsaved changes in the buffer. This only happens when autoSave is disabled and the form buffer is not empty.

Parameters: None.


Listening in Livewire

You can also listen for these events in other Livewire components using the #[On] attribute:


Markdown file generated by schenke-io/packaging-tools


All versions of livewire-auto-form with dependencies

PHP Build Version
Package Version
Requires php Version ^8.3
illuminate/contracts Version ^12.0 || ^13.0
illuminate/support Version ^12.0 || ^13.0
illuminate/validation Version ^12.0 || ^13.0
illuminate/routing Version ^12.0 || ^13.0
illuminate/http Version ^12.0 || ^13.0
spatie/laravel-package-tools Version ^1.0
archtechx/enums Version ^v1.1
spatie/laravel-data Version ^4.0
nette/neon Version ^3.4
livewire/flux Version ^2.1
livewire/livewire Version ^4.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package schenke-io/livewire-auto-form contains the following files

Loading the files please wait ...