Skip to content

Commit e3a1824

Browse files
Fix #19627 - Reset UNSIGNED attribute when changing to non-numeric co… (#20280)
* Fix #19627 - Reset UNSIGNED attribute when changing to non-numeric column type * Fix eslint array-element-newline violations * Trigger CI re-run * Update resources/js/modules/functions.ts --------- Signed-off-by: Nicolai Ehrhardt <245527909+predictor2718@users.noreply.github.com> Co-authored-by: Maurício Meneghini Fauth <mauricio@mfauth.com.br>
1 parent ea951ef commit e3a1824

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

resources/js/modules/functions.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,32 @@ export function checkTableEditForm (theForm, fieldsCnt) {
731731
}
732732
}
733733

734+
// Check for UNSIGNED/ZEROFILL on non-numeric types
735+
const $typeField = $('#field_' + i + '_2');
736+
const $attrField = $typeField.closest('tr').find('select[name^="field_attribute"]');
737+
const typeVal = String($typeField.val() || '').toUpperCase();
738+
const attrVal = String($attrField.val() || '').toUpperCase();
739+
if (attrVal === 'UNSIGNED' || attrVal === 'UNSIGNED ZEROFILL' || attrVal === 'ZEROFILL') {
740+
const numTypes = [
741+
'TINYINT',
742+
'SMALLINT',
743+
'MEDIUMINT',
744+
'INT',
745+
'BIGINT',
746+
'FLOAT',
747+
'DOUBLE',
748+
'DECIMAL',
749+
];
750+
elm3 = $('#field_' + i + '_1');
751+
if (numTypes.indexOf(typeVal) === -1 && elm3.val() !== '') {
752+
$attrField.trigger('select');
753+
ajaxShowMessage(typeVal + ' ' + window.Messages.strInvalidAttribute, null, 'error');
754+
$attrField.trigger('focus');
755+
756+
return false;
757+
}
758+
}
759+
734760
if (atLeastOneField === 0) {
735761
id = 'field_' + i + '_1';
736762
if (! emptyCheckTheField(theForm, id)) {
@@ -1570,6 +1596,37 @@ function showNoticeForEnum (selectElement) {
15701596
/**
15711597
* Hides/shows a warning message when LENGTH is used with inappropriate integer type
15721598
*/
1599+
/**
1600+
* Reset the attribute dropdown when the column type is changed
1601+
* to a non-numeric type. Attributes like UNSIGNED and ZEROFILL
1602+
* are only valid for numeric types.
1603+
*/
1604+
function resetAttributeForNonNumericType ($typeSelector) {
1605+
const type = String($typeSelector.val() || '').toUpperCase();
1606+
const numericTypes = [
1607+
'TINYINT',
1608+
'SMALLINT',
1609+
'MEDIUMINT',
1610+
'INT',
1611+
'BIGINT',
1612+
'FLOAT',
1613+
'DOUBLE',
1614+
'DECIMAL',
1615+
];
1616+
1617+
if (numericTypes.indexOf(type) === -1) {
1618+
// Find the attribute select in the same row
1619+
const $row = $typeSelector.closest('tr');
1620+
const $attrSelect = $row.find('select[name^="field_attribute"]');
1621+
const currentAttr = String($attrSelect.val() || '').toUpperCase();
1622+
if (currentAttr === 'UNSIGNED' || currentAttr === 'UNSIGNED ZEROFILL'
1623+
|| currentAttr === 'ZEROFILL'
1624+
) {
1625+
$attrSelect.val('');
1626+
}
1627+
}
1628+
}
1629+
15731630
function showWarningForIntTypes () {
15741631
if (! $('div#length_not_allowed').length) {
15751632
return;
@@ -2144,6 +2201,7 @@ export function onloadEnumSetEditorMessage (): void {
21442201
$(document).on('change', 'select.column_type', function () {
21452202
showNoticeForEnum($(this));
21462203
showWarningForIntTypes();
2204+
resetAttributeForNonNumericType($(this));
21472205
});
21482206

21492207
$(document).on('change', 'select.default_type', function () {

src/Controllers/JavaScriptMessagesController.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ private function getMessages(): array
116116
'strRadioUnchecked' => __('Select at least one of the options!'),
117117
'strEnterValidNumber' => __('Please enter a valid number!'),
118118
'strEnterValidLength' => __('Please enter a valid length!'),
119+
'strInvalidAttribute' => __('does not support the UNSIGNED attribute.'),
119120
'strAddIndex' => __('Add index'),
120121
'strEditIndex' => __('Edit index'),
121122
/* l10n: Rename a table Index */

0 commit comments

Comments
 (0)