Product screen input validation complete
Inputs are now validated and will now allow saving if they are bad.
This commit is contained in:
@@ -15,6 +15,15 @@ namespace C968Project.Views
|
||||
private ScreenOption _screenOption;
|
||||
private Product? _currentProduct;
|
||||
|
||||
private bool _isNameValid = false;
|
||||
private bool _isInventoryValid = false;
|
||||
private bool _isPriceValid = false;
|
||||
private bool _isMinValid = false;
|
||||
private bool _isMaxValid = false;
|
||||
|
||||
private int _validatedMin = 0;
|
||||
private int _validatedMax = 0;
|
||||
|
||||
|
||||
public AddModifyProductScreen(ScreenOption screenOption, Product? product = null)
|
||||
{
|
||||
@@ -35,6 +44,8 @@ namespace C968Project.Views
|
||||
}
|
||||
|
||||
allPartsDataGridView.DataSource = Program.Inventory.Parts;
|
||||
|
||||
ValidateInputs();
|
||||
}
|
||||
|
||||
private void PopulateProductData(Product product)
|
||||
@@ -106,16 +117,19 @@ namespace C968Project.Views
|
||||
|
||||
private void saveButton_Click(object sender, EventArgs e)
|
||||
{
|
||||
if (_screenOption is ScreenOption.ADD)
|
||||
if (ValidateInputs())
|
||||
{
|
||||
AddProduct();
|
||||
Close();
|
||||
}
|
||||
if (_screenOption is ScreenOption.ADD)
|
||||
{
|
||||
AddProduct();
|
||||
Close();
|
||||
}
|
||||
|
||||
if (_screenOption is ScreenOption.MODIFY)
|
||||
{
|
||||
ModifyProduct();
|
||||
Close();
|
||||
if (_screenOption is ScreenOption.MODIFY)
|
||||
{
|
||||
ModifyProduct();
|
||||
Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,7 +149,7 @@ namespace C968Project.Views
|
||||
Part part = Program.Inventory.LookupPart(allPartsDataGridView.CurrentCell.RowIndex);
|
||||
if (part is null) return;
|
||||
|
||||
|
||||
|
||||
|
||||
if (_currentProduct.AssociatedParts.Contains(part))
|
||||
{
|
||||
@@ -154,6 +168,7 @@ namespace C968Project.Views
|
||||
return;
|
||||
}
|
||||
|
||||
if (!Helpers.ConfirmDelete()) return;
|
||||
Part part = _currentProduct.LookupAssociatedPart(associatedPartsDataGridView.CurrentCell.RowIndex);
|
||||
|
||||
if (part is null || !_currentProduct.AssociatedParts.Contains(part))
|
||||
@@ -164,5 +179,152 @@ namespace C968Project.Views
|
||||
|
||||
_currentProduct.RemoveAssociatedPart(_currentProduct.AssociatedParts.IndexOf(part));
|
||||
}
|
||||
|
||||
private void nameTextBox_TextChanged(object sender, EventArgs e)
|
||||
{
|
||||
ValidateName();
|
||||
}
|
||||
|
||||
private void ValidateName()
|
||||
{
|
||||
if (nameTextBox.Text == string.Empty)
|
||||
{
|
||||
errorProvider1.SetError(nameTextBox, "Please enter a product name");
|
||||
_isNameValid = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
errorProvider1.SetError(nameTextBox, "");
|
||||
_isNameValid = true;
|
||||
}
|
||||
|
||||
Helpers.SetTextBoxValidationState(nameTextBox, _isNameValid);
|
||||
}
|
||||
|
||||
private void inventoryTextBox_TextChanged(object sender, EventArgs e)
|
||||
{
|
||||
ValidateInventory();
|
||||
}
|
||||
|
||||
private void ValidateInventory()
|
||||
{
|
||||
int inventory = 0;
|
||||
|
||||
if (inventoryTextBox.Text != string.Empty && int.TryParse(inventoryTextBox.Text, out inventory))
|
||||
{
|
||||
if (_isMinValid && _isMaxValid)
|
||||
{
|
||||
if (inventory > _validatedMax)
|
||||
{
|
||||
errorProvider1.SetError(inventoryTextBox, $"Must be less than Max: {_validatedMax}");
|
||||
_isInventoryValid = false;
|
||||
}
|
||||
else if (inventory < _validatedMin)
|
||||
{
|
||||
errorProvider1.SetError(inventoryTextBox, $"Must be greater than Min: {_validatedMin}");
|
||||
_isInventoryValid = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
errorProvider1.SetError(inventoryTextBox, "");
|
||||
_isInventoryValid = true;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
errorProvider1.SetError(inventoryTextBox, $"Min and Max values must be set");
|
||||
_isInventoryValid = false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_isInventoryValid = false;
|
||||
errorProvider1.SetError(inventoryTextBox, "Field must contain an integer value");
|
||||
}
|
||||
|
||||
Helpers.SetTextBoxValidationState(inventoryTextBox, _isInventoryValid);
|
||||
}
|
||||
|
||||
private void priceTextBox_TextChanged(object sender, EventArgs e)
|
||||
{
|
||||
ValidatePrice();
|
||||
}
|
||||
|
||||
private void ValidatePrice()
|
||||
{
|
||||
float price = 0f;
|
||||
|
||||
if (priceTextBox.Text != string.Empty && float.TryParse(priceTextBox.Text, out price))
|
||||
{
|
||||
_isPriceValid = true;
|
||||
errorProvider1.SetError(priceTextBox, "");
|
||||
}
|
||||
else
|
||||
{
|
||||
_isPriceValid = false;
|
||||
errorProvider1.SetError(priceTextBox, "Field must contain a floating point number");
|
||||
}
|
||||
|
||||
Helpers.SetTextBoxValidationState(priceTextBox, _isPriceValid);
|
||||
}
|
||||
|
||||
private void maxTextBox_TextChanged(object sender, EventArgs e)
|
||||
{
|
||||
ValidateMax();
|
||||
}
|
||||
|
||||
private void ValidateMax()
|
||||
{
|
||||
int max = 0;
|
||||
if (maxTextBox.Text != string.Empty && int.TryParse(maxTextBox.Text, out max))
|
||||
{
|
||||
_isMaxValid = true;
|
||||
errorProvider1.SetError(maxTextBox, "");
|
||||
_validatedMax = max;
|
||||
}
|
||||
else
|
||||
{
|
||||
_isMaxValid = false;
|
||||
errorProvider1.SetError(maxTextBox, "Field must contain an integer value");
|
||||
}
|
||||
|
||||
ValidateInventory();
|
||||
Helpers.SetTextBoxValidationState(maxTextBox, _isMaxValid);
|
||||
}
|
||||
|
||||
private void minTextBox_TextChanged(object sender, EventArgs e)
|
||||
{
|
||||
ValidateMin();
|
||||
}
|
||||
|
||||
private void ValidateMin()
|
||||
{
|
||||
int min = 0;
|
||||
if (minTextBox.Text != string.Empty && int.TryParse(minTextBox.Text, out min))
|
||||
{
|
||||
_isMinValid = true;
|
||||
errorProvider1.SetError(minTextBox, "");
|
||||
_validatedMin = min;
|
||||
}
|
||||
else
|
||||
{
|
||||
_isMinValid = false;
|
||||
errorProvider1.SetError(minTextBox, "Field must contain an integer value");
|
||||
}
|
||||
|
||||
ValidateInventory();
|
||||
Helpers.SetTextBoxValidationState(minTextBox, _isMinValid);
|
||||
}
|
||||
|
||||
private bool ValidateInputs()
|
||||
{
|
||||
ValidateName();
|
||||
ValidateInventory();
|
||||
ValidatePrice();
|
||||
ValidateMin();
|
||||
ValidateMax();
|
||||
|
||||
return _isNameValid && _isInventoryValid && _isPriceValid && _isMinValid && _isMaxValid;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user