Add negative validation for treshold [SCI-6743]

This commit is contained in:
Anton 2022-04-25 12:04:07 +02:00
parent 2b2888e6f7
commit b56f11fc18
4 changed files with 55 additions and 5 deletions

View file

@ -37,6 +37,19 @@ var MyModuleStockConsumption = (function() {
.text(formatDecimalValue(String(finalValue), $('#stock_consumption').data('decimals')));
$('.stock-final-container').toggleClass('negative', finalValue <= 0);
$(this).closest('.sci-input-container').toggleClass('error', !($(this).val().length && this.value >= 0));
if ($(this).val().length === 0) {
$(this).closest('.sci-input-container')
.attr(
'data-error-text',
I18n.t('repository_stock_values.manage_modal.amount_error')
);
} else if (this.value <= 0) {
$(this).closest('.sci-input-container')
.attr(
'data-error-text',
I18n.t('repository_stock_values.manage_modal.negative_error')
);
}
$('.update-consumption-button').attr('disabled', !($(this).val().length && this.value >= 0));
});

View file

@ -134,7 +134,6 @@ var RepositoryStockValues = (function() {
$('#reminder-selector-checkbox').on('change', function() {
let valueContainer = $('.repository-stock-reminder-value');
valueContainer.toggleClass('hidden', !this.checked);
valueContainer.find('input').attr('required', this.checked);
if (!this.checked) {
$(this).data('reminder-value', valueContainer.find('input').val());
valueContainer.find('input').val(null);
@ -163,14 +162,50 @@ var RepositoryStockValues = (function() {
} else {
dropdownSelector.hideError(UNIT_SELECTOR);
}
if ($('#stock-input-amount').val().length && $('#stock-input-amount').val() >= 0) {
$('#stock-input-amount').parent().removeClass('error');
let stockInput = $('#stock-input-amount');
if (stockInput.val().length && stockInput.val() >= 0) {
stockInput.parent().removeClass('error');
} else {
$('#stock-input-amount').parent().addClass('error');
stockInput.parent().addClass('error');
if (stockInput.val().length === 0) {
stockInput.parent()
.attr(
'data-error-text',
I18n.t('repository_stock_values.manage_modal.amount_error')
);
} else {
stockInput.parent()
.attr(
'data-error-text',
I18n.t('repository_stock_values.manage_modal.negative_error')
);
}
status = false;
}
let reminderInput = $('.repository-stock-reminder-value input');
if ($('#reminder-selector-checkbox')[0].checked) {
if (reminderInput.val().length && reminderInput.val() >= 0) {
reminderInput.parent().removeClass('error');
} else {
reminderInput.parent().addClass('error');
if (reminderInput.val().length === 0) {
reminderInput.parent()
.attr(
'data-error-text',
I18n.t('repository_stock_values.manage_modal.amount_error')
);
} else {
reminderInput.parent()
.attr(
'data-error-text',
I18n.t('repository_stock_values.manage_modal.negative_error')
);
}
status = false;
}
}
return status;
});

View file

@ -14,6 +14,7 @@ class RepositoryStockValue < ApplicationRecord
accepts_nested_attributes_for :repository_cell
validates :repository_cell, presence: true
validates :low_stock_threshold, numericality: { greater_than_or_equal_to: 0 }, allow_nil: true
after_create do
next if is_a?(RepositoryStockConsumptionValue)

View file

@ -1727,6 +1727,7 @@ en:
amount_placeholder: "Enter amount"
amount_placeholder_new: "100, 2000, ..."
amount_error: "Enter an amount"
negative_error: "Negative values are not allowed."
unit: "Unit"
unit_prompt: "Select unit"
unit_error: "Select a unit"