Skip to content

Commit

Permalink
commit inicial
Browse files Browse the repository at this point in the history
  • Loading branch information
masakik committed Oct 21, 2024
0 parents commit 2287419
Show file tree
Hide file tree
Showing 9 changed files with 571 additions and 0 deletions.
22 changes: 22 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "uspdev/forms",
"description": "Formulários dinâmicos com persistência",
"type": "library",
"minimum-stability": "dev",
"license": "GPL-2.0-or-later",
"autoload": {
"psr-4": {
"Uspdev\\Forms\\": "src/"
}
},
"authors": [
{
"name": "Masaki Kawabata Neto",
"email": "[email protected]"
}
],
"require": {
"php": "^8.2",
"illuminate/support": "^11.0"
}
}
11 changes: 11 additions & 0 deletions config/uspdev-forms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

return [
'bootstrapVersion' => 4, // Default Bootstrap version (4 or 5)
'formSize' => 'small', // small or empty

'defaultKey' => 'single-key-app', // default key for single key app
'defaultMethod'=> 'POST',
'defaultGroup'=> 'default-form-group',
'defaultBtnLabel' => 'Enviar',
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateFormDefinitionsTable extends Migration
{
public function up()
{
Schema::create('form_definitions', function (Blueprint $table) {
$table->comment('created by uspdev-laravel-forms');
$table->id();
$table->string('name')->unique();
$table->string('group');
$table->string('description')->nullable();
$table->json('fields')->nullable(); // JSON structure of the form
$table->timestamps();
});
}

public function down()
{
Schema::dropIfExists('form_definitions');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateFormSubmissionsTable extends Migration
{
public function up()
{
Schema::create('form_submissions', function (Blueprint $table) {
$table->id();
$table->foreignId('form_definition_id')->constrained();
$table->bigInteger('user_id')->nullable(); // user associated to this submission, if available
$table->string('key'); // Key for retrieve submission (controlled by application)
$table->json('data'); // Store submitted data in JSON
$table->timestamps();

$table->comment('created by uspdev-laravel-forms');
// $table->foreign('form_definition_id')->references('id')->on('form_submissions')->onDelete('cascade');
});
}

public function down()
{
Schema::dropIfExists('form_submissions');
}
}
130 changes: 130 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Forms

Forms é uma biblioteca **uspdev** que permite gerar formulários dinâmicos a partir de definição armazenado em banco de dados e opcionalmente persiste os resultados.

## Features

- Gera formulários a partide definição em BD;
- Supporta estilos em Bootstrap 4 e 5;
- Processa submissão dos formulários com validação e persistência;
- Integra com aplicações Laravel 11 em diante.

## Installation

1. **Instale a biblioteca por meio do Composer:**

```bash
composer require uspdev/forms
```

2. **Publish the configuration and migrations:**

Run the following commands to publish the package's configuration and migrations:

php artisan vendor:publish --provider="Uspdev\Forms\Providers\FormServiceProvider" --tag=config
php artisan vendor:publish --provider="Uspdev\Forms\Providers\FormServiceProvider" --tag=migrations


3. **Run migrations:**

After publishing, run the migrations to create the necessary tables:

php artisan migrate

## Configuration

You can customize the package's settings by modifying the config/forms.php file. Here, you can set the default Bootstrap version.

## Usage

1. **Crie uma entrada na tabela form_definitions**

```php
$form = [
'name' => 'contact_form', // chave de acesso ao form
'group' => config('uspdev-forms.defaultGroup'), // permite agrupar vários forms
'description' => 'A form for user inquiries.',
'fields' => [
[
[
'name' => 'name',
'type' => 'text',
'label' => 'Nome (text)',
'required' => true
],
[
'name' => 'email',
'type' => 'email',
'label' => 'Email (email)',
'required' => false
],
],
[
'name' => 'rating',
'type' => 'select',
'label' => 'Avaliação (select)',
'options' => [
'1', '2', '3', '4', '5'
]
],
[
'name' => 'message',
'type' => 'textarea',
'label' => 'Mensagem (textarea)',
],
],
];

FormDefinition::create($form);

```
2. **Generate the form in your view:**

Use the FormGenerator class to render the form in your Blade template:

use Uspdev\Forms\FormGenerator;

$formGenerator = new FormGenerator(storage_path('forms/contact_form.yaml'));
echo $formGenerator->generateForm();

3. **Handle form submissions:**

In your controller, handle the form submission by saving the data to the database:


public function submit(Request $request)
{
// Validate and store form data...
}


## Contributing

Contributions are welcome! Please follow these steps to contribute:

Fork the repository.
Create a new branch (git checkout -b feature/YourFeature).
Make your changes and commit them (git commit -m 'Add some feature').
Push to the branch (git push origin feature/YourFeature).
Create a new Pull Request.

## License

This package is licensed under the MIT License. See the LICENSE file for details.


### Summary of Contents
- **Package Overview**: Describes what the package does.
- **Features**: Highlights key functionalities.
- **Installation Steps**: Provides detailed installation instructions.
- **Configuration Details**: Guides on customizing settings.
- **Usage Examples**: Shows how to create a YAML form and use it in your application.
- **Contribution Guidelines**: Encourages contributions with clear steps.
- **License Information**: Indicates the licensing of the package.






Loading

0 comments on commit 2287419

Please sign in to comment.