Skip to content

Build Laravel Forms Faster: A Guide to Automating Forms with Formello

Tired of manual Laravel form coding? Learn how to generate forms in minutes, not hours, using Laravel Formello. This guide shows you how to automate Eloquent model forms with Bootstrap styling, validation, and more to supercharge your workflow

Dasun Kalupahana 3 min read 628 views 0 comments

Are you tired of the repetitive cycle of coding form fields, writing validation logic, and handling error messages for every new feature in your Laravel application? For developers seeking a middle ground between the overhead of a full admin panel and the tedium of manual coding, Laravel Formello emerges as a powerful and elegant solution. This package allows you to generate beautiful, functional forms directly from your Eloquent models, streamlining your workflow and boosting productivity.



What is Laravel Formello?

Formello is a Laravel package that automates the generation of forms. Instead of manually crafting forms with HTML, you can leverage your Eloquent models to define the structure of your forms. This not only saves time but also ensures consistency across your application

The developer behind Formello created it for those who “need to generate forms quickly without the overhead of a complete admin panel, but who also want a simpler, more intuitive API than more complex form libraries. It hits the sweet spot for rapid form creation in projects that don’t require heavy UI tooling.



✨ Key Features

Formello comes packed with features that make form handling a breeze:

  • Rapid Form Definition: Easily define forms using intuitive Laravel classes.
  • Automatic Rendering: Forms are automatically rendered in your Blade templates, complete with labels and inputs.
  • Diverse Field Types: Supports a wide range of fields including text, textarea, select, radio, checkboxes, toggle switches, date, datetime, and file uploads.
  • Built-in Bootstrap 5: Forms are styled with Bootstrap 5 out of the box, with Tailwind CSS support announced as coming soon.
  • Seamless Validation: Integrates directly with Laravel’s validation system.
  • Automatic Error Handling: Displays validation errors automatically, no need to code this manually.
  • Custom Widgets: Extend functionality with your own custom widgets.


🛠️ Getting Started with Laravel Formello

Integrating Formello into your project is straightforward. Here’s how you can get up and running in just a few steps.


Step 1: Installation

First, install the package via Composer:

composer require metalogico/laravel-formello

Next, publish the Formello assets to your public directory:

php artisan vendor:publish --tag=formello-assets

Step 2: Include Assets in Your Layout

To ensure Formello’s CSS and JavaScript load correctly, include its styles and scripts in your main Blade layout, typically just before the closing </body> tag.

<!DOCTYPE html>
<html>
<head>
    <!-- Include Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.8/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Formello CSS -->
    @formelloStyles
</head>
<body>

    @yield('content')

    <!-- Include Bootstrap JS bundle -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.7/dist/js/bootstrap.bundle.min.js"></script>
    <!-- Formello JS -->
    @formelloScripts
</body>
</html>


Building Your First Form: A Practical Example

Let’s walk through creating a form for a blog article to see Formello in action.


1. Create a Form Class

You can create a form class manually or use the handy Artisan command to generate one automatically based on your model’s fillable fields.

php artisan make:formello --model=Article

This command generates a form class in app/Forms/ArticleForm.php. Here’s an example of what that class might look like:

<?php

namespace App\Forms;

use App\Models\Category;
use App\Models\User;
use Metalogico\Formello\Formello;

class ArticleForm extends Formello
{
    // Define behavior for creating a new article
    protected function create(): array
    {
        return [
            'method' => 'POST',
            'action' => route('articles.store'),
        ];
    }

    // Define behavior for editing an existing article
    protected function edit(): array
    {
        return [
            'method' => 'PATCH',
            'action' => route('articles.update', $this->model->id),
        ];
    }

    // Define the form fields
    protected function fields(): array
    {
        return [
            'title' => [
                'label' => __('Title'),
            ],
            'content' => [
                'label' => __('Content'),
                'widget' => 'textarea', // Explicitly define as textarea
            ],
            'category_id' => [
                'label' => __('Category'),
                'widget' => 'select',
                'choices' => Category::pluck('name', 'id')->toArray(),
            ],
            'author_id' => [
                'label' => __('Author'),
                'widget' => 'select',
                'choices' => User::pluck('name', 'id')->toArray(),
            ],
        ];
    }
}

2. Use the Form in Your Controller

In your controller, pass the form instance to the view.

public function create()
{
    $form = new ArticleForm(Article::class);
    return view('articles.create', ['form' => $form]);
}

public function edit(Article $article)
{
    $form = new ArticleForm($article);
    return view('articles.edit', ['form' => $form]);
}

3. Render the Form in Your Blade Template

Finally, in your Blade view, rendering the form is as simple as a single method call.

@extends('layouts.app')

@section('content')
<div class="container">
    <h2>Create New Article</h2>
    {!! $form->render() !!}
</div>
@endsection

And just like that, you have a fully functional, styled form with all the necessary fields!



💡 Pro Tips for Advanced Usage

Conditional Logic for Create and Edit

Formello makes it easy to have different fields or logic for creating versus editing a record. Use the isCreating() and isEditing() methods within your form class.

For example, you might want a password field to be required only when creating a new user:

protected function fields(): array
{
    $fields = [
        'name' => ['label' => __('Name')],
        'email' => ['label' => __('Email')],
    ];

    if ($this->isCreating()) {
        $fields['password'] = [
            'label' => __('Password'),
            'widget' => 'password',
            'required' => true, // Only required on create
        ];
    }

    return $fields;
}


🚀 Conclusion

Laravel Formello is a testament to efficient and clean coding practices. By automating the repetitive aspects of form creation, it allows you to focus on what truly matters — building your application’s core business logic. Whether you’re prototyping a new idea or maintaining a large application, Formello can help you generate consistent, maintainable forms in minutes, not hours.

Ready to give it a try? Install Formello in your next Laravel project and feel the difference. You can learn more and view the source code on GitHub.

If you found this helpful, feel free to share or drop a comment. Happy coding with Laravel! 🧱✨

Free resources for this topic

Code snippets and fixes for this topic

Get one practical tip every week

New tutorials, fixes and free cheat sheets from DEV Talk, straight to your inbox. No spam, unsubscribe in one click.

Free download: Laravel interview questions (PDF)

Have a question about this post?

Ask the community: other developers and the author can answer.

Ask the community

Get notified about new articles

One click, no email needed. Turn it off anytime with the bell at the top.

Get new articles on your iPhone

Add DEV Talk to your Home Screen, open it, then tap the bell.

Rate this post
0.0
Share

Enjoying the content?

If this article helped you, you can support the author with a coffee. It keeps new posts coming.

Buy me a coffee (opens in a new tab)

Comments 0

Be kind. Comments with links are reviewed before they appear.

No comments yet

Be the first to share your thoughts.

Get new articles in your inbox

Practical tutorials and deep dives, about once a week. No spam, unsubscribe anytime.

Privacy policy

Add DEV Talk to your Home Screen

  1. 1 Tap the Share button in Safari's toolbar.
  2. 2 Scroll down and tap Add to Home Screen.
  3. 3 Tap Add. Open it from the Home Screen to read like an app and to turn on notifications with the bell.

We use cookies to improve your experience and analyze traffic. By continuing, you agree to our Privacy Policy.