PHP code example of haithammaznai7 / form-stepper

1. Go to this page and download the library: Download haithammaznai7/form-stepper library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

haithammaznai7 / form-stepper example snippets


'controller' => \App\Http\Controllers\FormStepperController::class,



namespace App\Forms;

use HaithamMaznai\FormStepper\Forms\FormBuilder;

class ContactFormBuilder extends FormBuilder
{
    public function formType(): string
    {
        return 'contact';
    }

    public function mode(): string
    {
        return 'stepper';
    }

    public function startWithSteps(): array
    {
        return [
            [
                'key' => 'contact-details',
                'priority' => 1,
                '

'builders' => [
    'contact' => App\Forms\ContactFormBuilder::class,
],

namespace App\Models;

use HaithamMaznai\FormStepper\Contracts\ProvidesFormRequirements;
use Illuminate\Database\Eloquent\Model;

class FormOption extends Model implements ProvidesFormRequirements
{
    protected $fillable = ['key', '  public function formOptionKey(): string { return $this->key; }
    public function formSteps(): array { return $this->

public function up(): void
{
    \Illuminate\Support\Facades\Schema::create('application_form_options', function (
        \Illuminate\Database\Schema\Blueprint $table,
    ): void {
        $table->id();
        $table->string('key')->unique();
        $table->json('ts('application_form_options');
}

use HaithamMaznai\FormStepper\Schema\Input;
use HaithamMaznai\FormStepper\Schema\Step;
use HaithamMaznai\FormStepper\Schema\Requirements;

$name = Input::make('name', attributes: [
    'rules' => [',
);

$deliveryRequirements = Requirements::make(
    Step::make('contact', [
        $name,
        Input::make('phone', attributes: ['rules' => ['

use App\Models\FormOption;

$inspection = FormOption::updateOrCreate(
    ['key' => 'inspection'],
    [
        'ry = FormOption::updateOrCreate(
    ['key' => 'delivery'],
    [
        '

public function formSteps(): array
{
    return \HaithamMaznai\FormStepper\Schema\Requirements::fromArray(
        $this->

use App\Models\InputDefinition;
use HaithamMaznai\FormStepper\Schema\Input;
use HaithamMaznai\FormStepper\Schema\Step;
use HaithamMaznai\FormStepper\Schema\Requirements;

$lookup = InputDefinition::updateOrCreate(
    ['key' => 'name'],
    ['definition' => Input::make('name', attributes: [
        'rules' => ['

public function resolveOptions(array $keys): array
{
    return \App\Models\FormOption::query()
        ->whereIn('key', $keys)
        ->get()
        ->all();
}

[
    'key' => 'address',
    'type' => 'complex',
    ' ['> ['nullable', 'string']],
    ],
]

[
    'key' => 'vehicles',
    'priority' => 100,
    'repeatable' => true,
    'repeat-name' => 'vehicles',
    '

'types-scope' => ['order'],
'tenant-types' => ['order'],
'requester-scope' => ['order'],
'guest-scope' => ['authenticated'],

'tenant' => [
    'enabled' => true,
    'relationship' => 'currentTenant',
],

public function currentTenant(): \Illuminate\Database\Eloquent\Relations\BelongsTo
{
    return $this->belongsTo(Team::class, 'current_tenant_id');
}

namespace App\Models;

use HaithamMaznai\FormStepper\Contracts\TenantForm;
use HaithamMaznai\FormStepper\Models\Form;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany;

class Team extends Model implements TenantForm
{
    /** @return MorphMany<Form, $this> */
    public function forms(): MorphMany
    {
        return $this->morphMany(Form::class, 'tenant');
    }
}

public function prefillValues(?\Illuminate\Database\Eloquent\Model $requester = null, ?\Illuminate\Database\Eloquent\Model $tenant = null): array
{
    return $requester === null ? [] : [
        'contact-details' => ['name' => $requester->getAttribute('name')],
    ];
}

public function defaultValues(?\Illuminate\Database\Eloquent\Model $requester = null, ?\Illuminate\Database\Eloquent\Model $tenant = null): array
{
    return [
        'contact-details' => [
            'name' => $requester?->getAttribute('name') ?? 'Guest',
            'address' => ['city' => 'Riyadh'],
        ],
        'vehicles' => ['plate' => ''], // defaults for the inputs of each repeat instance
    ];
}

Step::make('contact', [$name], ['priority' => 1]);
Step::make('documents', [], ['priority' => 100]);
Step::make('confirmation', [], ['priority' => -1]);

public function startWithSteps(): array
{
    return [Step::make('contact', [], ['priority' => 1])->toArray()];
}

public function endWithSteps(): array
{
    return [Step::make('confirmation', [], ['priority' => -1])->toArray()];
}

return [
    'defaults' => [
        'inputs' => [
            'name' => ['label' => 'Name', 'placeholder' => 'Enter your name'],
        ],
        'steps' => [
            'contact-details' => ['title' => 'Contact information', 'subtitle' => 'Your details'],
            'review' => ['title' => 'Review'],
        ],
    ],
    'contexts' => [
        'order' => [                     // form type, not input type
            'stepper' => [               // or single
                'customer' => [          // stored requester morph type, or guest
                    'team' => [          // stored tenant morph type, or none
                        'inputs' => [
                            'name' => ['label' => 'Customer name', 'placeholder' => 'Full name'],
                        ],
                        'steps' => [
                            'contact-details' => ['title' => 'Customer details'],
                        ],
                    ],
                ],
            ],
        ],
    ],
];

'resources' => [
    'form' => \HaithamMaznai\FormStepper\Http\Resources\FormResource::class,
    'step' => \App\Http\Resources\FormStepResource::class,
    '

namespace App\Http\Resources;

use HaithamMaznai\FormStepper\Http\Resources\StepResource;
use Illuminate\Http\Request;

class FormStepResource extends StepResource
{
    public function toArray(Request $request): array
    {
        return [...parent::toArray($request), 'icon' => $this->definition()['extra']['icon'] ?? null];
    }
}

'routes' => [
    'enabled' => true,
    'prefix' => 'forms-api',
    'middleware' => ['web'],
    'name' => 'form-stepper.forms.',
],

use App\Forms\ContactFormBuilder;
use HaithamMaznai\FormStepper\Services\FormService;

$builder = app(ContactFormBuilder::class);
$service = app(FormService::class);
$request = request();
$builder->authorize('create', $request);

$created = $service->create(
    $builder,
    [],
    $builder->requester($request),
    $builder->tenant($request),
);

$form = $created['form'];
$payload = $created['result']->toArray();
$saved = $service->saveStep($form, 'contact-details', [
    'name' => 'Ada',
    'email' => '[email protected]',
])->toArray();

// config/form-stepper.php
'admin' => [
    'enabled' => true,                     // routes are off by default
    'prefix' => 'form-stepper/admin',
    'middleware' => ['web', 'auth'],
    'name' => 'form-stepper.admin.',
    'gate' => 'manage-form-stepper',       // null disables the gate check
    'per_page' => 15,
    'controllers' => [ /* see step 4 */ ],
],

use Illuminate\Support\Facades\Gate;

Gate::define('manage-form-stepper', fn ($user) => $user->is_admin);

use HaithamMaznai\FormStepper\Models\FormInput;
use HaithamMaznai\FormStepper\Models\FormStepTemplate;
use HaithamMaznai\FormStepper\Schema\Requirements;
use HaithamMaznai\FormStepper\Schema\Step;

$contact = FormStepTemplate::where('key', 'contact')->firstOrFail();
$vehicle = FormInput::where('key', 'vehicle')->firstOrFail();

$inspection->update(['

'controllers' => [
    'input_types' => \App\Http\Controllers\FormStepper\Admin\InputTypeController::class,
    'inputs' => \App\Http\Controllers\FormStepper\Admin\InputController::class,
    'steps' => \App\Http\Controllers\FormStepper\Admin\StepTemplateController::class,
    'forms' => \App\Http\Controllers\FormStepper\Admin\FormController::class,
],
bash
php artisan vendor:publish --tag=form-stepper-config
php artisan vendor:publish --tag=form-stepper-migrations
bash
php artisan migrate
php artisan route:list --name=form-stepper.forms
bash
php artisan vendor:publish --tag=form-stepper-routes
php artisan vendor:publish --tag=form-stepper-controller
bash
php artisan vendor:publish --tag=form-stepper-lang
bash
php artisan vendor:publish --tag=form-stepper-views              # resources/views/vendor/form-stepper/admin/*
php artisan vendor:publish --tag=form-stepper-admin-controllers  # app/Http/Controllers/FormStepper/Admin/*