PHP code example of formpanel / formpanel-laravel

1. Go to this page and download the library: Download formpanel/formpanel-laravel 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/ */

    

formpanel / formpanel-laravel example snippets


use Formpanel\Laravel\Facades\Formpanel;

// Submit form data using the default form slug from config
$result = Formpanel::submit([
    'name' => 'John Doe',
    'email' => '[email protected]',
    'message' => 'Hello from Laravel!',
]);

echo "Submission ID: " . $result->id;

use Formpanel\Laravel\Facades\Formpanel;

// Get a client for a specific form
$client = Formpanel::form('contact-form');

// Submit data
$result = $client->submit([
    'name' => 'Jane Doe',
    'email' => '[email protected]',
    'message' => 'Hello!',
]);

// Get form information
$form = $client->get();
echo "Form: {$form->name}";
echo "Fields: " . count($form->fields);

use Formpanel\FormClient;

class ContactController extends Controller
{
    public function __construct(protected FormClient $formpanel)
    {
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => '     } catch (\Formpanel\Exceptions\ValidationException $e) {
            return redirect()->back()->withErrors($e->getErrors());
        }
    }
}

use Formpanel\Laravel\FormpanelFactory;

class FormController extends Controller
{
    public function __construct(protected FormpanelFactory $formpanel)
    {
    }

    public function submitContact(Request $request)
    {
        $client = $this->formpanel->make('contact-form');
        $result = $client->submit($request->validated());

        return redirect()->back()->with('success', 'Contact form submitted!');
    }

    public function submitNewsletter(Request $request)
    {
        $client = $this->formpanel->make('newsletter-signup');
        $result = $client->submit(['email' => $request->email]);

        return redirect()->back()->with('success', 'Subscribed to newsletter!');
    }
}

use Formpanel\Laravel\Facades\Formpanel;

$form = Formpanel::get();

echo "Form: {$form->name}\n";
echo "Status: {$form->status}\n";
echo "Description: {$form->description}\n";

foreach ($form->fields as $field) {
    echo "- {$field->name} ({$field->type})";
    echo $field->



namespace App\Http\Controllers;

use Formpanel\FormClient;
use Formpanel\Exceptions\ValidationException;
use Formpanel\Exceptions\FormpanelException;
use Illuminate\Http\Request;

class ContactController extends Controller
{
    public function __construct(protected FormClient $formpanel)
    {
    }

    public function create()
    {
        return view('contact');
    }

    public function store(Request $request)
    {
        $validated = $request->validate([
            'name' => '              ->withInput()
                ->withErrors($e->getErrors());

        } catch (FormpanelException $e) {
            // Other FormPanel errors
            return redirect()
                ->back()
                ->withInput()
                ->with('error', 'Failed to submit form: ' . $e->getMessage());
        }
    }
}



namespace App\Jobs;

use Formpanel\Laravel\FormpanelFactory;
use Formpanel\Exceptions\FormpanelException;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;

class SubmitFormToFormpanel implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    public function __construct(
        protected string $formSlug,
        protected array $data
    ) {
    }

    public function handle(FormpanelFactory $factory): void
    {
        try {
            $client = $factory->make($this->formSlug);
            $result = $client->submit($this->data);

            Log::info('Form submitted to FormPanel', [
                'submission_id' => $result->id,
                'form_slug' => $this->formSlug,
            ]);
        } catch (FormpanelException $e) {
            Log::error('Failed to submit form to FormPanel', [
                'error' => $e->getMessage(),
                'form_slug' => $this->formSlug,
            ]);

            throw $e;
        }
    }
}

use App\Jobs\SubmitFormToFormpanel;

SubmitFormToFormpanel::dispatch('contact-form', [
    'name' => 'John Doe',
    'email' => '[email protected]',
]);

use Formpanel\Laravel\Facades\Formpanel;
use Formpanel\Exceptions\AuthenticationException;
use Formpanel\Exceptions\ValidationException;
use Formpanel\Exceptions\NotFoundException;
use Formpanel\Exceptions\RateLimitException;
use Formpanel\Exceptions\FormpanelException;

try {
    $result = Formpanel::submit($data);

} catch (AuthenticationException $e) {
    // Invalid API key (401)
    Log::error('FormPanel authentication failed', ['error' => $e->getMessage()]);

} catch (NotFoundException $e) {
    // Form not found (404)
    Log::warning('FormPanel form not found', ['error' => $e->getMessage()]);

} catch (ValidationException $e) {
    // Validation errors (422)
    $errors = $e->getErrors();
    return back()->withErrors($errors);

} catch (RateLimitException $e) {
    // Rate limit exceeded (429)
    $retryAfter = $e->getRetryAfter();
    Log::warning('FormPanel rate limit exceeded', ['retry_after' => $retryAfter]);

} catch (FormpanelException $e) {
    // Other errors (network issues, parsing errors, etc.)
    Log::error('FormPanel error', ['error' => $e->getMessage()]);
}

$result = Formpanel::submit($data);

$result->id;       // string - Submission UUID
$result->success;  // bool - Whether submission was successful
$result->message;  // string - Success/error message

$form = Formpanel::get();

$form->id;          // string - Form UUID
$form->name;        // string - Form name
$form->slug;        // string - Form slug
$form->description; // ?string - Form description
$form->status;      // string - Form status (e.g., 'active')
$form->fields;      // FormField[] - Array of field definitions
$form->createdAt;   // DateTimeImmutable
$form->updatedAt;   // DateTimeImmutable

foreach ($form->fields as $field) {
    $field->name;        // string - Field name/identifier
    $field->label;       // string - Display label
    $field->type;        // string - Field type
    $field->

'retry' => [
    'max_retries' => 5,
    'base_delay_ms' => 500,
    'max_delay_ms' => 60000,
    'multiplier' => 2.0,
],

use Formpanel\FormClient;
use Formpanel\SubmissionResult;
use Mockery;

public function test_contact_form_submission()
{
    $mockClient = Mockery::mock(FormClient::class);
    $mockClient->shouldReceive('submit')
        ->once()
        ->with(Mockery::any())
        ->andReturn(new SubmissionResult(
            id: '550e8400-e29b-41d4-a716-446655440000',
            success: true,
            message: 'Form submitted successfully!',
        ));

    $this->app->instance(FormClient::class, $mockClient);

    $response = $this->post('/contact', [
        'name' => 'John Doe',
        'email' => '[email protected]',
        'message' => 'Test message',
    ]);

    $response->assertRedirect();
}

use Formpanel\FormClient;
use Formpanel\Http\HttpClientInterface;

$mockHttpClient = new class implements HttpClientInterface {
    public function get(string $uri): array {
        return ['id' => 'test', 'name' => 'Test Form', /* ... */];
    }

    public function post(string $uri, array $data): array {
        return ['submission_id' => 'test-123', 'success' => true, 'message' => 'OK'];
    }
};

$client = new FormClient(
    apiKey: 'test-key',
    formSlug: 'test-form',
    httpClient: $mockHttpClient,
);
bash
php artisan vendor:publish --tag=formpanel-config