PHP code example of metalogico / laravel-formello

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

    

metalogico / laravel-formello example snippets


// Before (v1.x)
protected function fields(): array
{
    return [
        'name' => [
            'label' => 'Name',
            'widget' => 'text',
        ],
    ];
}

// After (v2.x)
use Metalogico\Formello\FormelloField;

protected function fields(): array
{
    return [
        FormelloField::make('name')->label('Name')->widget('text'),
    ];
}



namespace App\Forms;

use App\Models\Category;
use Metalogico\Formello\Formello;
use Metalogico\Formello\FormelloField;

class ProductForm extends Formello
{
    protected function create(): array
    {
        return [
            'method' => 'POST',
            'action' => route('products.store'),
        ];
    }

    protected function edit(): array
    {
        return [
            'method' => 'PATCH',
            'action' => route('products.update', $this->model->id),
        ];
    }

    protected function fields(): array
    {
        return [
            FormelloField::make('name')
                ->label(__('Product Name'))
                ->help('Enter the name of the product')
                ->

class Product extends Model
{
    protected $fillable = [
        'name',
        'category_id',
        'description',
        'price',
        'in_stock',
    ];
}

public function create()
{
    $formello = new ProductForm(Product::class);

    return view('products.create', [
        'formello' => $formello,
    ]);
}

public function edit(Product $product)
{
    $formello = new ProductForm($product);

    return view('products.edit', [
        'formello' => $formello,
    ]);
}

protected function fields(): array
{
    $password_field = FormelloField::make('password')
        ->label(__('Password'))
        ->type('password');

    if ($this->isCreating()) {
        $password_field->          ->help('Enter the name of the user'),

        $password_field,
    ];
}

$formello = new ProductForm($product);
$formello->setCssFramework('tailwindcss4');

protected function fields(): array
{
    return [
        FormelloField::make('status')
            ->widget('select')
            ->choices(['active' => 'Active', 'other' => 'Other...'])
            ->reactive(['client' => 'onStatusChanged']),

        FormelloField::make('status_other')
            ->label('Specify'),
    ];
}

FormelloField::make('region_id')
    ->widget('tomselect')
    ->reactive(['server' => 'onRegionChanged']),

FormelloField::make('province_id')
    ->widget('tomselect'),

use Metalogico\Formello\Support\FormelloState;

public function onRegionChanged(FormelloState $state): void
{
    $region_id = $state->get('region_id');
    $provinces = Province::where('region_id', $region_id)
        ->pluck('name', 'id')->toArray();

    $state->setOptions('province_id', $provinces);
    $state->set('province_id', null);
}

'custom_widgets' => [
    'star-rating' => App\Widgets\StarRatingWidget::class,
],



namespace App\Widgets;

use Metalogico\Formello\Widgets\BaseWidget;

class StarRatingWidget extends BaseWidget
{
    public function getViewData(string $name, $value, array $config, array $errors): array
    {
        return [
            'name' => $name,
            'value' => $value,
            'config' => $config,
            'errors' => $errors,
        ];
    }

    public function getTemplate(): string
    {
        return 'widgets.star-rating';
    }
}

FormelloField::make('rating')
    ->label(__('Product Rating'))
    ->widget('star-rating'),

'assets' => [
    'tomselect' => false, // theme already has Tom Select
    'date' => true,
    'datetime' => true,
    'mask' => true,
    'color' => true,
    'colorswatch' => true,
    'wysiwyg' => true,
],
bash
php artisan vendor:publish --tag=formello-assets --force
php artisan vendor:publish --tag=formello-config --force
bash
php artisan vendor:publish --tag=formello-assets
json
"scripts": {
    "post-update-cmd": [
        "@php artisan vendor:publish --tag=formello-assets --force"
    ]
}
bash
php artisan make:formello --model=Product
blade
@foreach ($formello->getFields() as $name => $field)
    {!! $formello->renderField($name) !!}
@endforeach
bash
php artisan vendor:publish --tag=formello-config