Skip to content

Commit

Permalink
Fix: edition w/ nullable boolean and value map
Browse files Browse the repository at this point in the history
Set value was not selected in <select> on edition
  • Loading branch information
nboisteault authored and github-actions[bot] committed Sep 18, 2023
1 parent 96dd350 commit 8541f49
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
37 changes: 29 additions & 8 deletions lizmap/modules/lizmap/lib/Form/QgisFormControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,14 +506,35 @@ protected function fillControlDatasource()
case 'ValueMap':
$valueMap = $this->properties->getValueMap();
if (is_array($valueMap)) {
// remove the QGIS null value if the control value is not
// required, as the widget will have a possibility to choose
// no value (so null value... ?)
if (!$this->required
&& $this->fieldDataType == 'boolean'
&& isset($valueMap[self::QGIS_NULL_VALUE])
) {
unset($valueMap[self::QGIS_NULL_VALUE]);
// Override values for boolean
if ($this->fieldDataType == 'boolean') {
// remove the QGIS null value if the control value is not
// required, as the widget will have a possibility to choose
// no value (so null value... ?)
if (!$this->required
&& isset($valueMap[self::QGIS_NULL_VALUE])
) {
unset($valueMap[self::QGIS_NULL_VALUE]);
}
// transform values from QGIS ValueMap to Postgres Boolean
$booleanValues = array();
foreach ($valueMap as $v => $label) {
$strV = strtolower($v);
if ($v === self::QGIS_NULL_VALUE) {
$booleanValues[self::QGIS_NULL_VALUE] = $label;
} elseif ($strV === 'true' || $strV === 't'
|| intval($v) === 1 || $strV === 'on') {
// Postgres true
$booleanValues['t'] = $label;
} elseif ($strV === 'false' || $strV === 'f'
|| intval($v) === 0 || $strV === 'off') {
// Postgres false
$booleanValues['f'] = $label;
} else {
$booleanValues[$strV] = $label;
}
}
$valueMap = $booleanValues;
}
// we don't use array_merge, because this function reindexes keys if they are
// numerical values, and this is not what we want.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,6 @@ describe('Form edition all field type', function() {
cy.get('#lizmap-edition-message').should('be.visible')
})

it('boolean, dropdown menu', function () {
// `boolean_nullable` should show a dropdown menu with :
// * an empty value
cy.get('#jforms_view_edition_boolean_nullable').select('')
cy.get('#jforms_view_edition_boolean_nullable').should('have.value', '')
// * a true value
cy.get('#jforms_view_edition_boolean_nullable').select('True')
cy.get('#jforms_view_edition_boolean_nullable').should('have.value', 'true')
// * a false value
cy.get('#jforms_view_edition_boolean_nullable').select('False')
cy.get('#jforms_view_edition_boolean_nullable').should('have.value', 'false')
})

it('boolean, not null', function () {
cy.get('#jforms_view_edition_boolean_notnull_for_checkbox').should('have.class', 'jforms-ctrl-checkbox')
cy.get('#jforms_view_edition__submit_submit').click()
Expand Down
4 changes: 2 additions & 2 deletions tests/units/classes/Form/QgisFormControlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,9 @@ public function testConstructCheckbox()
$this->assertEquals($control->fieldDataType, 'boolean');
$this->assertEquals($control->fieldEditType, 'CheckBox');
$this->assertEquals($control->ctrl->getWidgetType(), 'checkbox');
$this->assertEquals($control->ctrl->valueOnCheck, 'true');
$this->assertEquals($control->ctrl->valueOnCheck, 't');
$this->assertEquals($control->ctrl->valueLabelOnCheck, 'Yes');
$this->assertEquals($control->ctrl->valueOnUncheck, 'false');
$this->assertEquals($control->ctrl->valueOnUncheck, 'f');
$this->assertEquals($control->ctrl->valueLabelOnUncheck, 'No');
}
}

0 comments on commit 8541f49

Please sign in to comment.