Giter Site home page Giter Site logo

laravel-inertia-form's Introduction

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

  1. Create a RequestForm class and extend it from InertiaFormRequest
  2. 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;
    }
}
  1. Use toInertia() method to convert the form to an array and send it to the frontend. And use validated() 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

  1. Clone the repository
  2. Run composer install
  3. Run composer test to run the tests
  4. Run composer pint to run the linter (syntax check)
  5. Run composer fix to fix the linter errors
  6. Run composer analyse to run phpstan analyse

License

The MIT License (MIT). Please see License File for more information.

laravel-inertia-form's People

Contributors

khodakhah avatar github-actions[bot] avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.