From 9515aa9d8c518aae63855bff38038cbd1b4990f7 Mon Sep 17 00:00:00 2001 From: Arif RH Date: Fri, 1 May 2020 07:02:02 +0700 Subject: [PATCH] validate fields from invalid --- src/Models/DynaModelTrait.php | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/Models/DynaModelTrait.php b/src/Models/DynaModelTrait.php index 53e7faf..4eefa41 100644 --- a/src/Models/DynaModelTrait.php +++ b/src/Models/DynaModelTrait.php @@ -244,6 +244,8 @@ public function findAll(int $limit = 0, int $offset = 0) */ protected function doProtectFields(array $data): array { + $data = $this->validateFields($data); + if (! empty($this->protectedFields)) { foreach ($data as $key => $val) @@ -254,9 +256,30 @@ protected function doProtectFields(array $data): array } } } + return parent::doProtectFields($data); } + /** + * Make sure to remove invalid fields + * + * @param mixed[] $data + * + * @return mixed[] + */ + protected function validateFields(array $data): array + { + foreach ($data as $key => $val) + { + if (! array_key_exists($key, $this->fieldInfo)) + { + unset($data[$key]); + } + } + + return $data; + } + /** * Set the value of allowedFields *