-
-
Notifications
You must be signed in to change notification settings - Fork 244
Add fieldset formwidget #1435
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add fieldset formwidget #1435
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
18113c3
add fieldset formwidget
mjauvin b53bc99
remove extra semicolon
mjauvin bebc32e
use label instead of placeholder for fieldset legend
mjauvin 9165f93
add missing docblock
mjauvin a96d8b8
Merge branch 'develop' into fieldset-formwidget
LukeTowers 2422ce1
Apply suggestion from @LukeTowers
LukeTowers b7cf609
only use outside fields as this is only for defining the grouped fields
mjauvin b7442e1
escape the label.
mjauvin b78bc05
fix codestyle
mjauvin 02a0fdd
we need fields, not just fields configs
mjauvin 9d552fc
Merge branch 'develop' into fieldset-formwidget
mjauvin 11faa45
add application exception for unsupported fields
mjauvin 9c885c0
add missing space after coma
mjauvin 116c95c
add support for formwidgets as fieldset internal fields
mjauvin f13f024
improve style
mjauvin f133d71
use switch statement
mjauvin 5458cae
does not save anything itself
mjauvin 4cfe2f8
only use fields config
mjauvin b6a5d3b
Add fields property to FieldSet class
AIC-BV dc73d97
remove unused $form property
mjauvin bd0f975
Merge branch 'develop' into fieldset-formwidget
LukeTowers cbba547
Fix broken darkmode on fieldset formwidget
LukeTowers eb4168c
Fix fieldset saving logic and add tests
LukeTowers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| <?php | ||
|
|
||
| namespace Backend\FormWidgets; | ||
|
|
||
| use Backend\Classes\FormField; | ||
| use Backend\Classes\FormWidgetBase; | ||
| use Backend\Widgets\Form; | ||
|
|
||
| /** | ||
| * FieldSet | ||
| * Renders a fieldset from multiple form fields. | ||
| * | ||
| * @package winter\wn-backend-module | ||
| * @author Marc Jauvin <marc.jauvin@gmail.com> | ||
| */ | ||
| class FieldSet extends FormWidgetBase | ||
| { | ||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| protected $defaultAlias = 'fieldset'; | ||
|
|
||
| /** | ||
| * @var array Field configuration | ||
| */ | ||
| public $fields; | ||
|
|
||
| /** | ||
| * @var bool Determines if this form field should display comments and labels. | ||
| */ | ||
| public $showLabels = false; | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * @var Form form widget reference | ||
| */ | ||
| protected $formWidget; | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function init() | ||
| { | ||
| $this->fillFromConfig([ | ||
| 'fields', | ||
| ]); | ||
|
|
||
| if ($this->formField->disabled) { | ||
| $this->previewMode = true; | ||
| } | ||
|
|
||
| $config = $this->makeConfig(['fields' => $this->fields]); | ||
| $config->model = $this->model; | ||
| $config->data = $this->getLoadValue(); | ||
| $config->alias = $this->alias . $this->defaultAlias; | ||
| // set arrayName from parent form to save fields to the model | ||
| $config->arrayName = $this->getParentForm()->arrayName; | ||
| $config->isNested = true; | ||
|
|
||
| $widget = $this->formWidget = $this->makeWidget(Form::class, $config); | ||
| $widget->previewMode = $this->previewMode; | ||
| $widget->bindToController(); | ||
| } | ||
|
|
||
| protected function loadAssets() | ||
| { | ||
| $this->addCss('css/fieldset.css', 'core'); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the save data for the nested fields, to be merged into the parent | ||
| * form's data as if these fields were defined at that level. Reusing the nested | ||
| * form's getSaveData() ensures number casting, widget getSaveValue() handling, | ||
| * NO_SAVE_DATA exclusion and disabled/hidden skipping all behave identically to | ||
| * a regular field. | ||
| */ | ||
| public function getSaveData(): array | ||
| { | ||
| return $this->formWidget->getSaveData(); | ||
| } | ||
|
|
||
| /** | ||
| * @inheritdoc | ||
| */ | ||
| public function render() | ||
| { | ||
| $this->prepareVars(); | ||
| return $this->makePartial('fieldset'); | ||
| } | ||
|
|
||
| public function prepareVars() | ||
| { | ||
| $this->formWidget->previewMode = $this->previewMode; | ||
| } | ||
|
|
||
| /** | ||
| * @inheritDoc | ||
| */ | ||
| public function getSaveValue($value) | ||
| { | ||
| return FormField::NO_SAVE_DATA; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| .fieldset{position:relative;min-height:30px;border:1px solid #d1d6d9;border-radius:0.5rem;box-shadow:inset 0 1px 0 rgba(209,214,217,0.25),0 1px 0 rgba(255,255,255,.5);margin-top:5px;padding:1em 1.25em 0 1.25em;background:#f5f5f5} | ||
| .fieldset>legend{border:none;margin:0;padding-inline:1rem;width:fit-content;font-size:18px;color:inherit} | ||
| .fieldset .control-tabs.primary-tabs .nav-tabs{margin:0} | ||
| .fieldset .control-tabs.primary-tabs .nav-tabs>li.active>a:before{background-color:#f5f5f5;border-color:#f5f5f5} | ||
| .fieldset .control-tabs.primary-tabs .nav-tabs>li>a>span.title:before, | ||
| .fieldset .control-tabs.primary-tabs .nav-tabs>li>a>span.title:after{background-color:#f5f5f5} |
41 changes: 41 additions & 0 deletions
41
modules/backend/formwidgets/fieldset/assets/less/fieldset.less
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| @import "../../../../assets/less/core/boot.less"; | ||
|
|
||
| @panel-bg: #f5f5f5; | ||
|
|
||
| .fieldset { | ||
| position: relative; | ||
| min-height: 30px; | ||
|
|
||
| border: 1px solid @input-border; | ||
| border-radius: 0.5rem; | ||
| box-shadow: @input-box-shadow; | ||
|
|
||
| margin-top: 5px; | ||
| padding: 1em 1.25em 0 1.25em; | ||
|
|
||
| background: @panel-bg; | ||
|
|
||
| & > legend { | ||
| border: none; | ||
| margin: 0; | ||
| padding-inline: 1rem; | ||
| width: fit-content; | ||
| font-size: 18px; | ||
| color: inherit; | ||
| } | ||
| .control-tabs.primary-tabs { | ||
| .nav-tabs { | ||
| margin: 0; | ||
| > li.active > a:before { | ||
| background-color: @panel-bg; | ||
| border-color: @panel-bg; | ||
| } | ||
|
|
||
| > li > a > span.title { | ||
| &:before, &:after { | ||
| background-color: @panel-bg; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
mjauvin marked this conversation as resolved.
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| <?php $label = object_get($this->config, 'label'); ?> | ||
|
|
||
| <fieldset class="fieldset"> | ||
| <?php if ($label): ?> | ||
| <legend><?= e(trans($label)) ?></legend> | ||
| <?php endif ?> | ||
|
|
||
| <?= $this->formWidget->render(['section' => 'outside']) ?> | ||
| </fieldset> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| <?php | ||
|
|
||
| namespace Backend\Tests\Widgets | ||
| { | ||
| use Backend\Classes\Controller; | ||
| use Backend\Classes\FormField; | ||
| use Backend\Classes\FormWidgetBase; | ||
| use Backend\Classes\WidgetManager; | ||
| use Backend\FormWidgets\FieldSet; | ||
| use System\Tests\Bootstrap\PluginTestCase; | ||
| use Winter\Storm\Database\Model; | ||
| use Backend\Widgets\Form; | ||
|
|
||
| class FormFieldSetSaveTestModel extends Model | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * A trivial form widget whose getSaveValue() observably transforms the posted value, | ||
| * used to prove that a fieldset's nested `widget` fields have getSaveValue() applied. | ||
| */ | ||
| class FieldSetSaveStubWidget extends FormWidgetBase | ||
| { | ||
| protected $defaultAlias = 'fieldsetsavestub'; | ||
|
|
||
| public function getSaveValue($value) | ||
| { | ||
| return is_string($value) ? strtoupper($value) : $value; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A nested form widget that opts out of saving (as e.g. FileUpload does, since its | ||
| * relation manages persistence). Its NO_SAVE_DATA sentinel must never reach the model. | ||
| */ | ||
| class FieldSetNoSaveStubWidget extends FormWidgetBase | ||
| { | ||
| protected $defaultAlias = 'fieldsetnosavestub'; | ||
|
|
||
| public function getSaveValue($value) | ||
| { | ||
| return FormField::NO_SAVE_DATA; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Covers Form::getSaveData()'s handling of the `fieldset` form widget: the fieldset | ||
| * visually groups fields but they must be saved as if they were regular fields at the | ||
| * parent level, while the fieldset container itself saves nothing. | ||
| */ | ||
| class FormFieldSetSaveTest extends PluginTestCase | ||
| { | ||
| public function setUp(): void | ||
| { | ||
| parent::setUp(); | ||
|
|
||
| // The backend module's form widgets are not auto-registered under PluginTestCase, | ||
| // so make the aliases used by the form field configs below resolvable. | ||
| WidgetManager::instance()->registerFormWidget(FieldSet::class, 'fieldset'); | ||
| WidgetManager::instance()->registerFormWidget(FieldSetSaveStubWidget::class, 'fieldsetsavestub'); | ||
| WidgetManager::instance()->registerFormWidget(FieldSetNoSaveStubWidget::class, 'fieldsetnosavestub'); | ||
| } | ||
|
|
||
| protected function makeForm(): Form | ||
| { | ||
| return new Form(new Controller, [ | ||
| 'model' => new FormFieldSetSaveTestModel, | ||
| 'arrayName' => 'array', | ||
| 'fields' => [ | ||
| 'top_level' => [ | ||
| 'type' => 'text', | ||
| ], | ||
| 'group' => [ | ||
| 'type' => 'fieldset', | ||
| 'label' => 'Grouped Fields', | ||
| 'fields' => [ | ||
| 'nested_text' => [ | ||
| 'type' => 'text', | ||
| ], | ||
| 'nested_number' => [ | ||
| 'type' => 'number', | ||
| ], | ||
| 'nested_widget' => [ | ||
| 'type' => 'fieldsetsavestub', | ||
| ], | ||
| 'nested_nosave' => [ | ||
| 'type' => 'fieldsetnosavestub', | ||
| ], | ||
| 'nested_disabled' => [ | ||
| 'type' => 'text', | ||
| 'disabled' => true, | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * getSaveData() reads its values with post(), which only returns real data when | ||
| * the request is genuinely a POST -- so simulate the postback first. | ||
| */ | ||
| protected function postForm(array $data): array | ||
| { | ||
| request()->setMethod('POST'); | ||
| request()->request->replace(['array' => $data]); | ||
|
|
||
| return $this->makeForm()->getSaveData(); | ||
| } | ||
|
|
||
| public function testNestedFieldSetFieldsAreCollectedToParentLevel() | ||
| { | ||
| $data = $this->postForm([ | ||
| 'top_level' => 'top', | ||
| 'nested_text' => 'hello', | ||
| 'nested_number' => '42', | ||
| ]); | ||
|
|
||
| // Nested fields are hoisted to the parent save data... | ||
| $this->assertArrayHasKey('nested_text', $data); | ||
| $this->assertArrayHasKey('nested_number', $data); | ||
| $this->assertEquals('hello', $data['nested_text']); | ||
|
|
||
| // ...and the top level field is unaffected. | ||
| $this->assertEquals('top', $data['top_level']); | ||
|
|
||
| // The fieldset container itself must not be saved. | ||
| $this->assertArrayNotHasKey('group', $data); | ||
| } | ||
|
|
||
| public function testNestedNumberFieldIsCastToFloat() | ||
| { | ||
| $data = $this->postForm([ | ||
| 'nested_number' => '42', | ||
| ]); | ||
|
|
||
| $this->assertSame(42.0, $data['nested_number']); | ||
| } | ||
|
|
||
| public function testNestedNumberEmptyStringBecomesNull() | ||
| { | ||
| $data = $this->postForm([ | ||
| 'nested_number' => ' ', | ||
| ]); | ||
|
|
||
| $this->assertArrayHasKey('nested_number', $data); | ||
| $this->assertNull($data['nested_number']); | ||
| } | ||
|
|
||
| public function testNestedWidgetFieldHasSaveValueApplied() | ||
| { | ||
| $data = $this->postForm([ | ||
| 'nested_widget' => 'abc', | ||
| ]); | ||
|
|
||
| // The nested widget's getSaveValue() must be applied (stub upper-cases it), | ||
| // rather than the raw posted value being stored. | ||
| $this->assertArrayHasKey('nested_widget', $data); | ||
| $this->assertSame('ABC', $data['nested_widget']); | ||
| } | ||
|
|
||
| public function testNestedWidgetReturningNoSaveDataDoesNotLeakSentinel() | ||
| { | ||
| $data = $this->postForm([ | ||
| 'nested_nosave' => 'ignore me', | ||
| ]); | ||
|
|
||
| // A nested widget that returns NO_SAVE_DATA must behave exactly as it would | ||
| // at the top level of a form: the NO_SAVE_DATA sentinel (-1) must never be | ||
| // written to the model. | ||
| $this->assertNotSame(FormField::NO_SAVE_DATA, $data['nested_nosave'] ?? null); | ||
| } | ||
|
|
||
| public function testDisabledNestedFieldIsNotSaved() | ||
| { | ||
| $data = $this->postForm([ | ||
| 'nested_text' => 'hello', | ||
| 'nested_disabled' => 'tampered', | ||
| ]); | ||
|
|
||
| // Disabled fields are omitted from the save data, just like top-level fields. | ||
| $this->assertArrayHasKey('nested_text', $data); | ||
| $this->assertArrayNotHasKey('nested_disabled', $data); | ||
| } | ||
|
|
||
| public function testMissingNestedValuesAreOmitted() | ||
| { | ||
| $data = $this->postForm([ | ||
| 'top_level' => 'top', | ||
| ]); | ||
|
|
||
| $this->assertArrayNotHasKey('nested_text', $data); | ||
| $this->assertArrayNotHasKey('nested_number', $data); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.