From f0765d10b58cea8027bfa00a796d6f8926f20ba8 Mon Sep 17 00:00:00 2001 From: lLefevre Date: Thu, 30 Aug 2012 16:06:09 +0000 Subject: [PATCH] [*] BO : #PSCFV-3831 : check each field which are required in databases --- classes/ObjectModel.php | 22 +++++++++++++++++++ .../admin/AdminAddressesController.php | 4 ++++ .../admin/AdminCustomersController.php | 4 ++++ controllers/front/AddressController.php | 3 +++ controllers/front/AuthController.php | 4 ++++ 5 files changed, 37 insertions(+) diff --git a/classes/ObjectModel.php b/classes/ObjectModel.php index 4e80b69e6..aac95ad72 100644 --- a/classes/ObjectModel.php +++ b/classes/ObjectModel.php @@ -1058,6 +1058,28 @@ abstract class ObjectModelCore return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query); } + public function validateFieldsRequiredDatabase($htmlentities = true) + { + $errors = array(); + $required_fields = (isset(self::$fieldsRequiredDatabase[get_class($this)])) ? self::$fieldsRequiredDatabase[get_class($this)] : array(); + + foreach ($this->def['fields'] as $field => $data) + { + if (!in_array($field, $required_fields)) + continue; + + if (!method_exists('Validate', $data['validate'])) + throw new PrestaShopException('Validation function not found. '.$data['validate']); + + $value = Tools::getValue($field); + + if (empty($value)) + $errors[] = sprintf(Tools::displayError('The field %s is required.'), self::displayFieldName($field, get_class($this), $htmlentities)); + } + + return $errors; + } + public function getFieldsRequiredDatabase($all = false) { return Db::getInstance()->executeS(' diff --git a/controllers/admin/AdminAddressesController.php b/controllers/admin/AdminAddressesController.php index 661d226ca..a4ca54a9a 100644 --- a/controllers/admin/AdminAddressesController.php +++ b/controllers/admin/AdminAddressesController.php @@ -370,6 +370,10 @@ class AdminAddressesControllerCore extends AdminController $_POST['id_address'] = ''; } + // Check the requires fields which are settings in the BO + $address = new Address(); + $this->errors = array_merge($this->errors, $address->validateFieldsRequiredDatabase()); + if (empty($this->errors)) return parent::processSave(); else diff --git a/controllers/admin/AdminCustomersController.php b/controllers/admin/AdminCustomersController.php index 25cd252eb..d8409d693 100644 --- a/controllers/admin/AdminCustomersController.php +++ b/controllers/admin/AdminCustomersController.php @@ -769,6 +769,10 @@ class AdminCustomersControllerCore extends AdminController if (!is_array(Tools::getValue('groupBox')) || !in_array(Tools::getValue('id_default_group'), Tools::getValue('groupBox'))) $this->errors[] = Tools::displayError('Default customer group must be selected in group box.'); + // Check the requires fields which are settings in the BO + $customer = new Customer(); + $this->errors = array_merge($this->errors, $customer->validateFieldsRequiredDatabase()); + return parent::processSave(); } diff --git a/controllers/front/AddressController.php b/controllers/front/AddressController.php index 2a128a206..48289d54f 100644 --- a/controllers/front/AddressController.php +++ b/controllers/front/AddressController.php @@ -182,6 +182,9 @@ class AddressControllerCore extends FrontController AND deleted = 0') > 0) $this->errors[] = sprintf(Tools::displayError('The alias "%s" is already used, please chose another one.'), Tools::safeOutput($_POST['alias'])); + // Check the requires fields which are settings in the BO + $this->errors = array_merge($this->errors, $address->validateFieldsRequiredDatabase()); + // Don't continue this process if we have errors ! if ($this->errors && !$this->ajax) return; diff --git a/controllers/front/AuthController.php b/controllers/front/AuthController.php index 4efb51c10..c6562ffd8 100644 --- a/controllers/front/AuthController.php +++ b/controllers/front/AuthController.php @@ -394,6 +394,10 @@ class AuthControllerCore extends FrontController if (!Tools::getValue('phone') && !Tools::getValue('phone_mobile') && Configuration::get('PS_REGISTRATION_PROCESS_TYPE')) $this->errors[] = Tools::displayError('You must register at least one phone number'); $this->errors = array_unique(array_merge($this->errors, $customer->validateController())); + + // Check the requires fields which are settings in the BO + $this->errors = array_merge($this->errors, $customer->validateFieldsRequiredDatabase()); + if (!Configuration::get('PS_REGISTRATION_PROCESS_TYPE') && !$this->ajax && !Tools::isSubmit('submitGuestAccount')) { if (!count($this->errors))