PHP code example of aerni / livewire-forms

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

    

aerni / livewire-forms example snippets


protected array $models = [
    \Statamic\Fieldtypes\Select::class => \App\Fields\Select::class,
];

protected array $models = [
    'products' => \App\Fields\SelectProduct::class,
];

public function mountedFields(Collection $fields): void
{
    $fields->get('name')->display('Your name');
}

public function formSubmitted(Submission $submission): void
{
    $title = $submission->augmentedValue('entry')->value()->title;

    $submission->set('entry_title', $title);
}

public function handle(FormSubmitted $event)
{
    $event->submission; // The Submission object
}

namespace App\Fields;

use Aerni\LivewireForms\Fields\Select;
use Statamic\Facades\Entry;

class SelectProduct extends Select
{
    protected string $view = 'select_product';

    public function optionsProperty(): array
    {
        return Entry::whereCollection('products')
            ->mapWithKeys(fn ($product) => [$product->slug() => $product->get('title')])
            ->all();
    }
}

namespace App\Livewire;

use Aerni\LivewireForms\Livewire\Form;

class ContactForm extends Form
{
    protected array $models = [
        'products' => \App\Fields\SelectProduct::class,
    ];
}

namespace App\Livewire;

use Aerni\LivewireForms\Livewire\Form;

class ContactForm extends Form
{
    public function mountedFields(Collection $fields): void
    {
        $options = Entry::whereCollection('products')
            ->mapWithKeys(fn ($product) => [$product->slug() => $product->get('title')])
            ->all();

        $fields->get('products')
            ->options($options)
            ->view('select_product');
    }
}

protected $messages = [
    'fields.name.value.

protected function messages(): array
{
    return [
        'fields.name.value.

return [

    'contact' => [
        'submit_button_label' => 'Contact now',
        'success_message' => 'Thanks for contacting us. We will be in touch.',
        'error_message' => 'There was an error with your submission:|There were :count errors with your submission:',
    ],

    'newsletter' => [
        'submit_button_label' => 'Signup now',
    ],

];
bash
php please vendor:publish --tag=livewire-forms-config
bash
php please livewire-forms:setup
blade
<!-- Antlers -->
{{ livewire:form handle="contact" }}

<!-- Blade -->
<livewire:form handle="contact">
blade
<!-- Antlers -->
{{ livewire:form :handle="field:handle" }}

<!-- Blade -->
<livewire:form :handle="field:handle">
blade
<!-- Antlers -->
{{ livewire:form :handle="field:handle" view="contact" theme="regular" }}

<!-- Blade -->
<livewire:form :handle="field:handle" view="contact" theme="regular">
bash
php please livewire-forms:view
blade
{{ $field->display }}
{{ $field->tooltip }}
bash
php please livewire-forms:theme
bash
php please livewire-forms:component ContactForm
blade
<!-- Antlers -->
{{ livewire:form handle="contact" }}

<!-- Blade -->
<livewire:form handle="contact">
blade
<!-- Antlers -->
{{ livewire:form handle="contact" }}

<!-- Blade -->
<livewire:form handle="contact">
bash
php artisan vendor:publish --tag=livewire-forms-translations