diff --git a/resources/js/modules/functions.ts b/resources/js/modules/functions.ts index f9572d771d3..86354a81793 100644 --- a/resources/js/modules/functions.ts +++ b/resources/js/modules/functions.ts @@ -731,6 +731,32 @@ export function checkTableEditForm (theForm, fieldsCnt) { } } + // Check for UNSIGNED/ZEROFILL on non-numeric types + const $typeField = $('#field_' + i + '_2'); + const $attrField = $typeField.closest('tr').find('select[name^="field_attribute"]'); + const typeVal = String($typeField.val() || '').toUpperCase(); + const attrVal = String($attrField.val() || '').toUpperCase(); + if (attrVal === 'UNSIGNED' || attrVal === 'UNSIGNED ZEROFILL' || attrVal === 'ZEROFILL') { + const numTypes = [ + 'TINYINT', + 'SMALLINT', + 'MEDIUMINT', + 'INT', + 'BIGINT', + 'FLOAT', + 'DOUBLE', + 'DECIMAL', + ]; + elm3 = $('#field_' + i + '_1'); + if (numTypes.indexOf(typeVal) === -1 && elm3.val() !== '') { + $attrField.trigger('select'); + ajaxShowMessage(typeVal + ' ' + window.Messages.strInvalidAttribute, null, 'error'); + $attrField.trigger('focus'); + + return false; + } + } + if (atLeastOneField === 0) { id = 'field_' + i + '_1'; if (! emptyCheckTheField(theForm, id)) { @@ -1570,6 +1596,37 @@ function showNoticeForEnum (selectElement) { /** * Hides/shows a warning message when LENGTH is used with inappropriate integer type */ +/** + * Reset the attribute dropdown when the column type is changed + * to a non-numeric type. Attributes like UNSIGNED and ZEROFILL + * are only valid for numeric types. + */ +function resetAttributeForNonNumericType ($typeSelector) { + const type = String($typeSelector.val() || '').toUpperCase(); + const numericTypes = [ + 'TINYINT', + 'SMALLINT', + 'MEDIUMINT', + 'INT', + 'BIGINT', + 'FLOAT', + 'DOUBLE', + 'DECIMAL', + ]; + + if (numericTypes.indexOf(type) === -1) { + // Find the attribute select in the same row + const $row = $typeSelector.closest('tr'); + const $attrSelect = $row.find('select[name^="field_attribute"]'); + const currentAttr = String($attrSelect.val() || '').toUpperCase(); + if (currentAttr === 'UNSIGNED' || currentAttr === 'UNSIGNED ZEROFILL' + || currentAttr === 'ZEROFILL' + ) { + $attrSelect.val(''); + } + } +} + function showWarningForIntTypes () { if (! $('div#length_not_allowed').length) { return; @@ -2144,6 +2201,7 @@ export function onloadEnumSetEditorMessage (): void { $(document).on('change', 'select.column_type', function () { showNoticeForEnum($(this)); showWarningForIntTypes(); + resetAttributeForNonNumericType($(this)); }); $(document).on('change', 'select.default_type', function () { diff --git a/src/Controllers/JavaScriptMessagesController.php b/src/Controllers/JavaScriptMessagesController.php index 0e9b04e808e..424593c68ee 100644 --- a/src/Controllers/JavaScriptMessagesController.php +++ b/src/Controllers/JavaScriptMessagesController.php @@ -116,6 +116,7 @@ private function getMessages(): array 'strRadioUnchecked' => __('Select at least one of the options!'), 'strEnterValidNumber' => __('Please enter a valid number!'), 'strEnterValidLength' => __('Please enter a valid length!'), + 'strInvalidAttribute' => __('does not support the UNSIGNED attribute.'), 'strAddIndex' => __('Add index'), 'strEditIndex' => __('Edit index'), /* l10n: Rename a table Index */