khodakhah / laravel-inertia-form
A package for Inertia.js forms integration in Laravel
v0.0.1-alpha
2023-09-02 20:17 UTC
Requires
- php: >=8.1
- illuminate/support: ^v10.20
- inertiajs/inertia-laravel: ^v0.6.9
- spatie/laravel-data: ^3.8
Requires (Dev)
- laravel/pint: ^v1.11
- nunomaduro/larastan: ^v2.6
- orchestra/testbench: ^v8.9
- pestphp/pest: ^v2.16
- pestphp/pest-plugin-laravel: ^v2.2
This package is auto-updated.
Last update: 2024-11-09 13:41:04 UTC
README
A simple package to handle forms in Laravel with InertiaJS.
This is the backend pair of InertiaForm package.
Requirements
- PHP ^8.1 || ^8.2
Installation
composer require khodakhah/larave-inertia-form
Usage
1. Create your form request class by extending InertiaFormRequest
- Create a RequestForm class and extend it from InertiaFormRequest
- Create a static method named formInputs and return an instance of InertiaForm
UserFormRequest.php
use Khodakhah\InertiaForm\InertiaFormRequest; class UserFormRequest extends InertiaFormRequest { public static function formInputs(\Khodakhah\InertiaForm\InertiaForm $form) : \Khodakhah\InertiaForm\InertiaForm{ $form->text('name', 'required'); $form->text('email', 'required|email'); $form->text('password', 'required|min:8'); return $form; } }
- Use
toInertia()
method to convert the form to an array and send it to the frontend. And usevalidated()
method to convert the form errors to an array and send it to the frontend.
class UserController extends Controller { use Khodakhah\InertiaForm\InertiaFormRequest; public function create() { return Inertia::render('User/Create', [ 'form' => UserFormRequest::toInertia() ]); } public function store(UserFormRequest $request) { User::create($request->validated()); return redirect()->route('users.index'); } }
2. Create your form object simply by using InertiaForm
You can simply create a form by using InertiaForm
class, and assign it into a variable, or return it from a method.
class UserController extends Controller { use Khodakhah\InertiaForm\InertiaForm; use Illuminate\Http\Request; private function userForm(): InertiaForm { $form = new InertiaForm(); $form->text('name', 'required'); $form->text('email', 'required|email'); $form->text('password', 'required|min:8'); return $form; } public function create() { return Inertia::render('User/Create', [ 'form' => $this->userForm()->toInertia() ]); } public function store(Request $request) { User::create( $request->validate( $this->userForm()->toValidation() ) ); return redirect()->route('users.index'); } }
Issues
If you have any issues, please create an issue in the issues section.
Contributing
If you have any ideas or suggestions, please create a pull request in the pull requests section. I'll be happy to review and merge them.
Local development
- Clone the repository
- Run
composer install
- Run
composer test
to run the tests - Run
composer pint
to run the linter (syntax check) - Run
composer fix
to fix the linter errors - Run
composer analyse
to run phpstan analyse
License
The MIT License (MIT). Please see License File for more information.