As of Twill 3.x you can, as an alternative to blade form files, define your forms from the module controller.
There are benefits and drawbacks to using the form builder or the form blade files:
Benefits
Drawbacks
To create a form from code you can add the getForm, getCreateForm or getSideFieldsets method to your controller.
The getForm, getCreateForm, getSideFieldsets method should return a Form object as illustrated below:
1<?php 2 3namespace App\Http\Controllers\Twill; 4 5use A17\Twill\Http\Controllers\Admin\ModuleController; 6use A17\Twill\Models\Contracts\TwillModelContract; 7use A17\Twill\Services\Forms\Form; 8 9 10class PageController extends ModuleController11{12 protected $moduleName = 'pages';13 14 public function getForm(TwillModelContract $model): Form15 {16 return Form::make();17 }18 19 public function getCreateForm(): Form20 {21 return Form::make();22 }23 24 public function getSideFieldsets(TwillModelContract $model): Form25 {26 return new Form();27 }28}
The Form object behind the scenes is a laravel Collection, this means you can use
collection methods to add fields.
1$form = Form::make();2$form->add(Input::make()->name('description'));
All form fields extend the BaseFormField class and have the same property options as explained in
the form field docs.
However, their names in some cases might be slightly different.
Fields in most cases require a name and label, but you can simply use name only as the label will be
automatically set, but will not be translatable.
1Input::make()2 ->name('description'); // Label: Description3Input::make()4 ->name('some_text'); // Label: Some text
If you want translated field labels you should use:
1Input::make()2 ->name('description')3 ->label(twillTrans('Description'))
It is also possible to add fieldsets to your form.
There are 2 Form methods that you can do this with:
withFieldSets
1$form->withFieldSets(new Fieldsets([2 Fieldset::make()->title('Fieldset 1')->id('fieldset')->fields([3 // Field definitions come here.4 ]),5 Fieldset::make()->title('Fieldset 2')->id('fieldset')->fields([6 // Field definitions come here.7 ])8]));
Or if you need more control:
addFieldset
1$form->addFieldset(2 Fieldset::make()->title('Fieldset!')->id('fieldset')->fields([3 // Field definitions come here.4 ])5);
For example...
1$form->addFieldset(2 Fieldset::make()->title('Opengraph')->id('opengraph')->fields([3 Input::make()->name('og_title')->label('OG Title'),4 Input::make()->name('og_description')->label('OG Description'),5 Files::make()->name('og_image')->label('OG Image')6 ])7);
Using the A17\Twill\Services\Forms\Columns you can add a left/right split:
1Columns::make() 2 ->left([ 3 Input::make() 4 ->name('description') 5 ->translatable(), 6 ]) 7 ->right([ 8 Input::make() 9 ->name('subtitle')10 ->translatable(),11 ]),
You can also inject a blade file (which can hold a form in itself) as a field using A17\Twill\Services\Forms\BladePartial:
1BladePartial::make()->view('twill.fields.field-under-condition')