Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdallaMohammed committed Jan 4, 2021
0 parents commit 40c2115
Show file tree
Hide file tree
Showing 13 changed files with 1,011 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
.phpunit.result.cache
composer.lock
vendor
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
language: php

php:
- 7.4
- 8.0

before_script:
- composer self-update

install:
- composer install --prefer-source --no-interaction --dev

script: vendor/bin/phpunit
15 changes: 15 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
The MIT License (MIT)
Copyright (c) 2017 Nexmo, Inc

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
153 changes: 153 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
## Laravel Multistep Form


[![Build Status](https://travis-ci.org/AbdallaMohammed/laravel-multistep-forms.svg?branch=master)](https://travis-ci.org/AbdallaMohammed/laravel-multistep-forms)

* [Installation](#installation)
* [Example Usage](#example-usage)
* [Steps Usage](#steps-usage)
* [Before](#before-step)
* [After](#after-step)
* [Dynamic](#dynamic-step)
* [Helper Methods](#helper-methods)

### Installation

```shell script
composer require abdallahmohammed/laravel-multistep-forms
```

### Example Usage

```php
use AbdallaMohammed\Form\Form;
use Illuminate\Support\Facades\Route;

Route::get('form', function () {
return app(Form::class)->make(function (Form $form) {
// Create a step instance and define rules, messages and attributes
$form->step()->rules([
'name' => ['required', 'string'],
])->messages([
'required' => ':attribute Required',
])->attributes([
'name' => 'Name',
]);

// Add another step with dynamic rules
$form->step()->dynamicRules();
});
})->name('form');
```

### Steps Usage

#### Before Step

Define a callback to fired **before** a step has been validated.

> Return a response from this hook to return early before validation occurs.
`before($step, Closure $closure)`

> $step could be Step instance, or the number of the step.
#### After Step

Define a callback to fired **after** a step has been validated. Step Number or * for all.

> Return a response from this hook to return early before the form step is incremented.
`after($step, Closure $closure)`

> $step could be Step instance, or the number of the step.
#### Dynamic Step

You can set a step as dynamic, so the step will take it's **rules**, **messages** and **attributes** from the request.

For example

```php
use AbdallaMohammed\Form\Form;
use Illuminate\Support\Facades\Route;

Route::get('form', function () {
return app(Form::class)->make(function (Form $form) {
...
$form->step()->dynamicRules()->messages([
'foo' => 'bar',
]);
...
});
})->name('form');
```

From the example we have defined the **attributes** without the **rules**, so we must send the rules with the request.
Here it is the example of the request body.

```json
{
"step": 1,
"1.rules": {
"name": ["required", "string"]
}
}
```

**1.rules** is a reference to first step rules.

> You can change **1** to the number of the dynamic step.
As the previous example you can send **1.messages** and **1.attributes** in the request body.

### Helper Methods

#### `stepConfig(?int $step = null)`

Get the current step config, or a specific step config.

#### `getValue(string $key, $fallback = null)`

Get a field value from the form state (session / old input) or fallback to a default.

#### `setValue(string $key, $value)`

Set a field value from the session form state.

#### `currentStep()`

Get the current saved step number.

#### `requestedStep()`

Get the requested step number.

#### `isStep(int $step = 1)`

Get the current step number.

#### `isLastStep()`

Determine if the current step the last step.

#### `isPast(int $step, $truthy = true, $falsy = false)`

Determine if the specified step is in the past.

#### `isActive(int $step, $truthy = true, $falsy = false)`

Determine if the specified step is active.

#### `isNext(int $step, $truthy = true, $falsy = false)`

Determine if the specified step is in the next.


#### `toCollection`

Get the array representation of the form state as a collection.

#### `toArray`

Get the array representation of the form state.
35 changes: 35 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "abdallahmohammed/laravel-multistep-forms",
"description": "Laravel Multistep Forms Builder",
"license": "MIT",
"authors": [
{
"name": "AbdallahMohammed",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"require": {
"php": "^7.4|^8.0",
"illuminate/http": "^6.0|^7.0|^8.0",
"illuminate/support": "^6.0|^7.0|^8.0",
"illuminate/session": "^6.0|^7.0|^8.0",
"illuminate/contracts": "^6.0|^7.0|^8.0",
"illuminate/validation": "^6.0|^7.0|^8.0"
},
"require-dev": {
"phpunit/phpunit": "^8.0|^9.0",
"orchestra/testbench": "^5.0|^6.0",
"nunomaduro/larastan": "^0.6"
},
"autoload": {
"psr-4": {
"AbdallaMohammed\\Forms\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"AbdallaMohammed\\Forms\\Tests\\": "tests"
}
}
}
6 changes: 6 additions & 0 deletions config/laravel-multistep-forms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

return [
'enable_data_saving' => env('SESSION_FORM_DATA_SAVING', true),
'session_name' => env('FORM_SESSION_NAME', 'multistep-forms'),
];
29 changes: 29 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<filter>
<whitelist addUncoveredFilesFromWhitelist="false">
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<php>
<env name="APP_KEY" value="base64:H+rWLjQ9vBGr1gQIhNz+XL5abPSAwjq420e41xPpaYY="/>
<env name="APP_ENV" value="testing"/>
<env name="APP_URL" value="http://localhost"/>
<env name="DB_DATABASE" value=":memory:"/>
<env name="DB_CONNECTION" value="sqlite"/>
</php>
</phpunit>
Loading

0 comments on commit 40c2115

Please sign in to comment.