From a15daee0da7b751aa08571a7d3530ca2ed66715a Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Fri, 7 Feb 2014 11:26:06 +0200 Subject: [PATCH] simple validation --- application/Espo/Services/Record.php | 30 ++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/application/Espo/Services/Record.php b/application/Espo/Services/Record.php index 2c4f536209..97978dd7cb 100644 --- a/application/Espo/Services/Record.php +++ b/application/Espo/Services/Record.php @@ -5,6 +5,7 @@ namespace Espo\Services; use \Espo\ORM\Entity; use \Espo\Core\Exceptions\Error; use \Espo\Core\Exceptions\Forbidden; +use \Espo\Core\Exceptions\BadRequest; use \Espo\Core\Utils\Util; class Record extends \Espo\Core\Services\Base @@ -156,13 +157,27 @@ class Record extends \Espo\Core\Services\Base { return $this->getRepository()->save($entity); } + + protected function isValid($entity) + { + $fieldDefs = $entity->getFields(); + if ($entity->hasField('name') && !empty($fieldDefs['name']['required'])) { + if (!$entity->get('name')) { + return false; + } + } + return true; + } public function createEntity($data) { - // TODO validate $data $entity = $this->getEntity(); - $entity->set($data); + $entity->set($data); + + if (!$this->isValid($entity)) { + throw new BadRequest(); + } if ($this->storeEntity($entity)) { return $entity; @@ -170,11 +185,10 @@ class Record extends \Espo\Core\Services\Base throw new Error(); } - + + public function updateEntity($id, $data) - { - // TODO validate $data - + { unset($data['deleted']); $entity = $this->getEntity($id); @@ -185,6 +199,10 @@ class Record extends \Espo\Core\Services\Base $entity->set($data); + if (!$this->isValid($entity)) { + throw new BadRequest(); + } + $d = $entity->get('attachmentsIds'); if ($this->storeEntity($entity)) {