rayhan-kobir/dynamic-form-generator

A dynamic form generator with grid support for Laravel

v1.0.0 2025-03-20 22:22 UTC

This package is auto-updated.

Last update: 2025-04-24 11:16:34 UTC


README

The Dynamic Form Generator is a Laravel package designed to simplify form creation by generating forms dynamically from a configuration array. It supports multiple input types, Bootstrap grid layouts, and integrates seamlessly with Laravel's features like CSRF protection and validation.

Installation

Run the following command in your terminal to install the package:

composer require rayhan-kobir/dynamic-form-generator

Verify Service Provider Registration

The package uses Laravel's auto-discovery to register its service provider. If auto-discovery is disabled in your project, manually add the provider to the providers array in config/app.php:

'providers' => [
    // Other providers...
    Rayhan\DynamicFormGenerator\DynamicFormGeneratorServiceProvider::class,
],

How to use in blade file

 @rayhanDynamicForm([
                        'action' => 'contact.submit',
                        'method' => 'POST',
                        'fields' => [
                            ['type' => 'text', 'name' => 'full_name', 'label' => 'Full Name', 'class' => 'form--control', 'col' => 'col-md-6','required' => true],
                            ['type' => 'email', 'name' => 'email', 'label' => 'Email Address', 'class' => 'form--control', 'col' => 'col-md-6'],
                            ['type' => 'password', 'name' => 'password', 'label' => 'Password', 'class' => 'form--control', 'col' => 'col-md-6'],
                            ['type' => 'number', 'name' => 'age', 'label' => 'Your Age', 'class' => 'form--control', 'col' => 'col-md-6'],
                            ['type' => 'date', 'name' => 'dob', 'label' => 'Date of Birth', 'class' => 'form--control', 'col' => 'col-md-6'],
                            ['type' => 'range', 'name' => 'experience', 'label' => 'Experience Level', 'min' => 1, 'max' => 10, 'class' => 'form--control', 'col' => 'col-md-6'],
                            ['type' => 'hidden', 'name' => 'token', 'value' => 'xyz123'],
                            ['type' => 'textarea', 'name' => 'address', 'label' => 'Your Address', 'class' => 'form--control', 'col' => 'col-md-12'],
                            [
                                'type' => 'select',
                                'name' => 'country',
                                'label' => 'Select Country',
                                'class' => 'form--control',
                                'col' => 'col-md-6',
                                'options' => [
                                    'bd' => 'Bangladesh',
                                    'us' => 'United States',
                                    'uk' => 'United Kingdom',
                                    'ca' => 'Canada',
                                ],
                            ],
                            [
                                'type' => 'radio',
                                'name' => 'gender',
                                'label' => 'Gender',
                                'class' => 'form-check-input',
                                'col' => 'col-md-6',
                                'options' => [
                                    'male' => 'Male',
                                    'female' => 'Female',
                                    'other' => 'Other',
                                ],
                            ],
                            [
                                'type' => 'checkbox',
                                'name' => 'hobbies[]',
                                'label' => 'Select Your Hobbies',
                                'class' => 'form-check-input',
                                'col' => 'col-md-6',
                                'options' => [
                                    'reading' => 'Reading',
                                    'sports' => 'Sports',
                                    'music' => 'Music',
                                ],
                            ],
                            ['type' => 'file', 'name' => 'profile_picture', 'label' => 'Upload Profile Picture', 'class' => 'form--control', 'col' => 'col-md-6'],
                        ],
                        'buttons' => [['type' => 'submit', 'label' => 'Submit', 'class' => 'btn btn--base'], ['type' => 'reset', 'label' => 'Reset', 'class' => 'btn btn--danger']],
                    ])

How to use validation in a controller?

  $formConfig = json_decode($request->input('form_config'), true);
        $form = new DynamicFormGenerator($formConfig);
        $validationResult = $form->validate($request);

        if ($validationResult !== true) {
            return $validationResult; 
        }