Skip to content

Commit

Permalink
Fix text type field validation. Handle upsert entity with errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioannis committed Feb 24, 2025
1 parent 7ee01a0 commit 66a2b91
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
7 changes: 7 additions & 0 deletions app/src/Lib/Traits/UpsertTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
namespace App\Lib\Traits;

trait UpsertTrait {
use \App\Lib\Traits\LabeledLogTrait;

/**
* Perform an upsert.
*
Expand Down Expand Up @@ -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);
}

Expand Down
16 changes: 11 additions & 5 deletions app/src/Lib/Traits/ValidationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 66a2b91

Please sign in to comment.