--- title: FormRenderer Component --- # FormRenderer Component The FormRenderer component displays forms to end users and handles submission processing. ## Basic Usage ```blade {{-- By slug (recommended) --}} {{-- By form ID --}} {{-- With form model --}} ``` ## Properties | Property | Type | Default | Description | |----------|------|---------|-------------| | `slug` | string\|null | null | Form slug | | `formId` | int\|null | null | Form ID | | `form` | Form\|null | null | Form model | | `successMessage` | string\|null | null | Override success message | | `redirectUrl` | string\|null | null | Redirect after submit | | `showTitle` | bool | true | Show form title | | `showDescription` | bool | true | Show form description | | `cssClass` | string | '' | Additional CSS classes | ## Examples ### Basic Form ```blade ``` ### With Custom Success Message ```blade ``` ### With Redirect ```blade ``` ### Minimal Display ```blade ``` ## Component State The component manages several states: ```php public array $formData = []; // Field values public array $errors = []; // Validation errors public bool $submitted = false; // Submission state public int $currentStep = 1; // Current step (multi-step) ``` ## Events Emitted | Event | Payload | Description | |-------|---------|-------------| | `form-submitted` | `{ formId, submissionId }` | Form submitted | | `step-changed` | `{ step }` | Step navigation | | `validation-failed` | `{ errors }` | Validation errors | ### JavaScript Listeners ```javascript document.addEventListener('livewire:initialized', () => { Livewire.on('form-submitted', (event) => { // Track conversion gtag('event', 'conversion', { send_to: 'AW-XXXXX/XXXXX', form_id: event.formId, }); }); Livewire.on('validation-failed', (event) => { // Scroll to first error const firstError = document.querySelector('.field-error'); firstError?.scrollIntoView({ behavior: 'smooth' }); }); }); ``` ## Methods ### submit Validates and submits the form: ```php public function submit(): void { $this->validate($this->getValidationRules()); $submission = $this->submissionService->create( $this->form, $this->formData ); if ($this->redirectUrl) { $this->redirect($this->redirectUrl); } else { $this->submitted = true; } $this->dispatch('form-submitted', [ 'formId' => $this->form->id, 'submissionId' => $submission->id, ]); } ``` ### nextStep / previousStep Navigate multi-step forms: ```php public function nextStep(): void { $this->validateCurrentStep(); $this->currentStep++; $this->dispatch('step-changed', step: $this->currentStep); } public function previousStep(): void { $this->currentStep--; $this->dispatch('step-changed', step: $this->currentStep); } ``` ## Validation Validation rules are generated from field configuration: ```php protected function getValidationRules(): array { $rules = []; foreach ($this->getVisibleFields() as $field) { $fieldRules = []; if ($field->required) { $fieldRules[] = 'required'; } if ($field->validation['min'] ?? null) { $fieldRules[] = 'min:' . $field->validation['min']; } if ($field->validation['max'] ?? null) { $fieldRules[] = 'max:' . $field->validation['max']; } // Add type-specific rules if ($field->type === 'email') { $fieldRules[] = 'email'; } $rules["formData.{$field->name}"] = $fieldRules; } return $rules; } ``` ## Customizing the View Publish and edit: ```bash php artisan vendor:publish --tag=forms-views ``` ### View Structure ```blade {{-- resources/views/vendor/forms/livewire/form-renderer.blade.php --}}
@if ($submitted) {{-- Success State --}}
{{ $successMessage ?? $form->success_message ?? 'Thank you!' }}
@else {{-- Form --}}
@if ($showTitle && $form->name)

{{ $form->name }}

@endif @if ($showDescription && $form->description)

{{ $form->description }}

@endif {{-- Progress (multi-step) --}} @if ($form->is_multi_step) @include('forms::partials.progress') @endif {{-- Fields --}} @foreach ($currentFields as $field) @include("forms::partials.fields.{$field->type}", ['field' => $field]) @endforeach {{-- Navigation --}} @include('forms::partials.navigation')
@endif
``` ### Custom Field Templates Create custom field templates in `resources/views/vendor/forms/partials/fields/`: ```blade {{-- custom-field.blade.php --}}
@if ($field->help_text) {{ $field->help_text }} @endif @error('formData.' . $field->name) {{ $message }} @enderror
``` ## File Uploads Handle file uploads with Livewire: ```blade {{-- file.blade.php --}}
settings['multiple'] ?? false) multiple @endif >
Uploading...
``` ## Accessibility The component includes accessibility features: - Proper label associations - ARIA attributes for errors - Focus management - Screen reader announcements ```blade @if ($field->help_text) {{ $field->help_text }} @endif @error('formData.' . $field->name) {{ $message }} @enderror ``` ## Extending the Component ```php namespace App\Livewire; use ArtisanPackUI\Forms\Livewire\FormRenderer; class CustomFormRenderer extends FormRenderer { public function submit(): void { // Pre-submission hook $this->preSubmit(); parent::submit(); // Post-submission hook $this->postSubmit(); } protected function preSubmit(): void { // Add custom data $this->formData['submitted_from'] = request()->url(); } } ``` ## Next Steps - [Form Renderer Usage](../usage/form-renderer.md) - Usage guide - [FormsList](./forms-list.md) - Forms listing component - [Submissions](../usage/submissions.md) - Submission handling