From f503ce057ef8bb32eb1b63c9c0328f40e135b75c Mon Sep 17 00:00:00 2001 From: Chris Alfano Date: Thu, 11 Jul 2019 10:36:47 -0400 Subject: [PATCH 1/2] feat: add beforeRecordTransaction event that can prepend validation --- php-classes/RecordsRequestHandler.class.php | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/php-classes/RecordsRequestHandler.class.php b/php-classes/RecordsRequestHandler.class.php index ae892025b..f33bd2e86 100644 --- a/php-classes/RecordsRequestHandler.class.php +++ b/php-classes/RecordsRequestHandler.class.php @@ -581,12 +581,24 @@ public static function handleEditRequest(ActiveRecord $Record) if ($_SERVER['REQUEST_METHOD'] == 'POST') { // apply delta static::applyRecordDelta($Record, $_REQUEST); + + // create space for transaction-level errors + $transactionErrors = []; + + // fire event + Emergence\EventBus::fireEvent('beforeRecordTransaction', $className::getRootClass(), array( + 'Record' => $Record, + 'data' => $_REQUEST, + 'transactionErrors' => &$transactionErrors + )); // call template function static::onBeforeRecordValidated($Record, $_REQUEST); // validate - if ($Record->validate()) { + if (!empty($transactionErrors)) { + $Record->addValidationErrors($transactionErrors); + } elseif ($Record->validate()) { // call template function static::onBeforeRecordSaved($Record, $_REQUEST); From 6f14ba3343145bbf7511b2d4d4e4c60af8c00cce Mon Sep 17 00:00:00 2001 From: Mike Gunn Date: Wed, 6 Jan 2021 16:25:00 -0500 Subject: [PATCH 2/2] refactor: validation error handling --- php-classes/RecordsRequestHandler.class.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/php-classes/RecordsRequestHandler.class.php b/php-classes/RecordsRequestHandler.class.php index f33bd2e86..13a0926c4 100644 --- a/php-classes/RecordsRequestHandler.class.php +++ b/php-classes/RecordsRequestHandler.class.php @@ -414,6 +414,8 @@ public static function handleMultiSaveRequest() // try to save record try { + // TODO: throw record validation exception if $Record->validationErrors is pre-populated + // call template function static::onBeforeRecordSaved($Record, $datum); @@ -582,14 +584,10 @@ public static function handleEditRequest(ActiveRecord $Record) // apply delta static::applyRecordDelta($Record, $_REQUEST); - // create space for transaction-level errors - $transactionErrors = []; - // fire event Emergence\EventBus::fireEvent('beforeRecordTransaction', $className::getRootClass(), array( 'Record' => $Record, - 'data' => $_REQUEST, - 'transactionErrors' => &$transactionErrors + 'data' => $_REQUEST )); // call template function @@ -598,7 +596,7 @@ public static function handleEditRequest(ActiveRecord $Record) // validate if (!empty($transactionErrors)) { $Record->addValidationErrors($transactionErrors); - } elseif ($Record->validate()) { + } elseif (!count($Record->validationErrors) && $Record->validate()) { // call template function static::onBeforeRecordSaved($Record, $_REQUEST);