Skip to content

More complex form

Alexey Dobrovolsky edited this page Sep 13, 2020 · 2 revisions

Elements types

Every form element extends from Dobrik\LaravelEasyForm\Forms\HtmlAbstract, and every object knows who it's parent object.
Also some html object implements interfaces:

Dobrik\LaravelEasyForm\Forms\Interfaces\HasContentInterface
Dobrik\LaravelEasyForm\Forms\Interfaces\HasValueInterface

Dobrik\LaravelEasyForm\Forms\Interfaces\HasContentInterface implemented by div, form, tab and template html objects, this objects contains content and can contains child elements. Dobrik\LaravelEasyForm\Forms\Interfaces\HasValueInterface implemented by input, select, textarea and image html objects, this object containt current value and can't contain any content or child elements.

Nested elements

If your form require nesting or more complex structure you can use 'child' element which accept array of array of elements. As example form configuration with two inputs with col-md-6 class in other div with row class:

    'profile' => [
        'child' => [
            'type' => 'div',
            'class' => 'row',
            'child' => [
                [
                    'type' => 'div',
                    'class' => 'col-md-6',
                    'child' => [
                        [
                            'type' => 'Input:text',
                            'title' => 'First name',
                            'name' => 'first_name',
                        ],
                    ]
                ],
                [
                    'type' => 'div',
                    'class' => 'col-md-6',
                    'child' => [
                        [
                            'type' => 'Input:text',
                            'title' => 'Last name',
                            'name' => 'last_name',
                        ],
                    ]
                ],
            ]
        ]
    ]

Select values

From the box package has select html type, this type requires list of values. values key of select html type handled by Dobrik\LaravelEasyForm\Handlers\SelectValuesHandler, this handler calls setValues method of HtmlAbstract object and set values key in $attributes property. You have few ways to fill list of available values. You can set values in form configuration key as value and value as label.

Predefined values

As example form element configuration with predefined values:

[
    'type' => 'select',
    'title' => 'Role',
    'name' => 'role_id',
    'values' => [
        'admin' => 'Administrator',
        'user' => 'User',
        'guest' => 'Guest',
    ]
]

Values from database

Or you can specify table which contains needle values:

[
    'type' => 'select',
    'title' => 'Role',
    'name' => 'role_id',
    'values' => '(roles|id,name)',
]

Where ($table|$value,$label).

Using filter objects

Filters object implements Dobrik\LaravelEasyForm\Forms\Interfaces\FilterInterface and handled by Dobrik\LaravelEasyForm\Handlers\FiltersHandler. This object allows to set some html object attributes, also set values for current select.

Select configuration example:

[
    'type' => 'select',
    'title' => 'Role',
    'name' => 'role_id',
    'filters' => [
        \App\Forms\Filters\RolesSelectFilter::class
    ],
]

And filter object:

<?php

namespace App\Form\Filters;

use Dobrik\LaravelEasyForm\Forms\HtmlAbstract;
use Dobrik\LaravelEasyForm\Forms\Interfaces\FilterInterface;


class RolesSelectFilter implements FilterInterface
{
    public function apply(HtmlAbstract $htmlAbstract, array $data = []): void
    {
        $htmlAbstract->setValues(Roles::query()->pluck('name', 'id')->toArray());
    }
}

Keep in mind $data array in apply filter method contains data only for existing data source. In filter objects you can write more complex logic to display list of values or set any additional data for any input type.

Using callback

You can use callback functions to proceed your html element using callback element key and callback function as value. All callbacks handled by Dobrik\LaravelEasyForm\Handlers\CallbackHandler first argument is HtmlAbstract object, second is array of data. Example:

[
    'type' => 'select',
    'title' => 'Role',
    'name' => 'role_id',
    'callback' => function(HtmlAbstract $htmlAbstract, array $data) {
        $htmlAbstract->setValues(Roles::query()->pluck('name', 'id')->toArray());
    }
]

You should keep in your mind about configuration caching, with using closure Laravel don't cache configuration. Best way it's using Filters.

If you create existing entity form and your data source (Model) contains role_id in values list, selected option automatically will have selected attribute.