commit 9a27c8a6f9d619349459c12d0afe9f489908ad02
parent 5312a73510c2fd50eadb2fa83e20e1d6270ca961
Author: Christian Grothoff <christian@grothoff.org>
Date: Sat, 11 Oct 2025 17:06:09 +0200
add minimum price category validation
Diffstat:
1 file changed, 35 insertions(+), 1 deletion(-)
diff --git a/src/Form/PriceCategoryForm.php b/src/Form/PriceCategoryForm.php
@@ -108,9 +108,11 @@ class PriceCategoryForm extends EntityForm {
$currency_label = $currency['label'] ?? $currency['code'];
$form['prices'][$subscription_id][$currency_code] = [
- '#type' => 'textfield',
+ '#type' => 'number',
'#title' => $currency_label,
'#default_value' => $existing_prices[$subscription_id][$currency_code] ?? '',
+ '#min' => 0,
+ // '#step' => 0.01, // FIXME: should this be set?
'#size' => 20,
'#description' => $this->t('Leave empty for no price.'),
];
@@ -120,6 +122,38 @@ class PriceCategoryForm extends EntityForm {
return $form;
}
+
+ /**
+ * {@inheritdoc}
+ */
+ public function validateForm(array &$form, FormStateInterface $form_state) {
+ parent::validateForm($form, $form_state);
+
+ $prices = $form_state->getValue('prices');
+
+ if (is_array($prices)) {
+ foreach ($prices as $subscription_id => $currencies) {
+ if (is_array($currencies)) {
+ foreach ($currencies as $currency_code => $price) {
+ // Skip empty values as they are allowed.
+ if ($price === '' || $price === NULL) {
+ continue;
+ }
+
+ // Validate that the price is a valid non-negative number.
+ if (!is_numeric($price) || $price < 0) {
+ $form_state->setErrorByName(
+ "prices[$subscription_id][$currency_code]",
+ $this->t('Prices cannot be negative.')
+ );
+ }
+ }
+ }
+ }
+ }
+ }
+
+
/**
* {@inheritdoc}
*/