From 66a2b91f9ec3c8b2edcd7a19f6c924b78412f51c Mon Sep 17 00:00:00 2001 From: Ioannis Igoumenos Date: Mon, 24 Feb 2025 15:21:03 +0200 Subject: [PATCH] Fix text type field validation. Handle upsert entity with errors. --- app/src/Lib/Traits/UpsertTrait.php | 7 +++++++ app/src/Lib/Traits/ValidationTrait.php | 16 +++++++++++----- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/app/src/Lib/Traits/UpsertTrait.php b/app/src/Lib/Traits/UpsertTrait.php index 449c4c772..f258dcee2 100644 --- a/app/src/Lib/Traits/UpsertTrait.php +++ b/app/src/Lib/Traits/UpsertTrait.php @@ -30,6 +30,8 @@ namespace App\Lib\Traits; trait UpsertTrait { + use \App\Lib\Traits\LabeledLogTrait; + /** * Perform an upsert. * @@ -59,6 +61,11 @@ public function upsert( $entity = $this->newEntity($data); } + if (!empty($entity->getErrors())) { + $this->llog('error', "Save failed for {$this->getAlias()}: " . print_r($entity->getErrors(), true)); + throw new \RuntimeException(__d('error', 'save', [$this->getAlias()])); + } + return $this->save($entity); } diff --git a/app/src/Lib/Traits/ValidationTrait.php b/app/src/Lib/Traits/ValidationTrait.php index 8ef1b8d0f..63e4992cc 100644 --- a/app/src/Lib/Traits/ValidationTrait.php +++ b/app/src/Lib/Traits/ValidationTrait.php @@ -274,21 +274,27 @@ public function validateInput($value, array $context) { return true; } - + /** * Validate the maximum length of a field. * + * @param string $value Value to validate + * @param array $context Validation context, which must include the schema definition + * + * @return bool|string True if $value validates, or an error string otherwise * @since COmanage Registry v5.0.0 - * @param string $value Value to validate - * @param array $context Validation context, which must include the schema definition - * @return mixed True if $value validates, or an error string otherwise */ - public function validateMaxLength($value, array $context) { + public function validateMaxLength(string $value, array $context): bool|string { // We use our own so we can introspect the field's max length from the // provided table schema object, and use our own error message (without // having to copy it to every table definition). + // Text has no limit. + if ($context['column']['type'] === 'text') { + return true; + } + $maxLength = $context['column']['length']; if(!empty($value) && strlen($value) > $maxLength) {