/** * Validates the current form. * * @throws class_exception * @return bool */ public function validateForm() { $objLang = class_carrier::getInstance()->getObjLang(); //1. Validate fields foreach ($this->arrFields as $objOneField) { $bitFieldIsEmpty = !is_array($objOneField->getStrValue()) && trim($objOneField->getStrValue()) === "" || is_null($objOneField->getStrValue()) || is_array($objOneField->getStrValue()) && count($objOneField->getStrValue()) == 0; //if it is an array with no entries //mandatory field if ($objOneField->getBitMandatory()) { //if field is mandatory and empty -> validation error if ($bitFieldIsEmpty) { $this->addValidationError($objOneField->getStrEntryName(), $objLang->getLang("commons_validator_field_empty", "system", array($objOneField->getStrLabel()))); } } //if field is not empty -> validate if (!$bitFieldIsEmpty) { if (!$objOneField->validateValue()) { $this->addValidationError($objOneField->getStrEntryName(), $objOneField->getStrValidationErrorMsg()); } } } //2. Validate complete object if ($this->getObjSourceobject() != null) { $objReflection = new class_reflection($this->getObjSourceobject()); $arrObjectValidator = $objReflection->getAnnotationValuesFromClass(self::STR_OBJECTVALIDATOR_ANNOTATION); if (count($arrObjectValidator) == 1) { $strObjectValidator = $arrObjectValidator[0]; if (!class_exists($strObjectValidator)) { throw new class_exception("object validator " . $strObjectValidator . " not existing", class_exception::$level_ERROR); } /** @var class_objectvalidator_base $objValidator */ $objValidator = new $strObjectValidator(); //Keep the reference of the current object $objSourceObjectTemp = $this->getObjSourceobject(); //Create a new instance of the source object and set it as source object in the formgenerator //Each existing field will also reference the new created source object $strClassName = get_class($this->objSourceobject); $this->objSourceobject = new $strClassName($this->objSourceobject->getStrSystemid()); foreach ($this->arrFields as $objOneField) { if ($objOneField->getObjSourceObject() != null) { $objOneField->setObjSourceObject($this->objSourceobject); } } //if we are in new-mode, we should fix the prev-id to the lateron matching one if ($this->getField("mode") != null && $this->getField("mode")->getStrValue() == "new" || class_carrier::getInstance()->getParam("mode") == "new") { $this->objSourceobject->setStrPrevId(class_carrier::getInstance()->getParam("systemid")); } //Update the new source object values from the fields and validate the object $this->updateSourceObject(); $objValidator->validateObject($this->getObjSourceobject()); foreach ($objValidator->getArrValidationMessages() as $strKey => $arrMessages) { if (!is_array($arrMessages)) { throw new class_exception("method validateObject must return an array of format array(\"<messageKey>\" => array())", class_exception::$level_ERROR); } foreach ($arrMessages as $strMessage) { $this->addValidationError($strKey, $strMessage); } } //Set back kept reference to the formgenerator and all it's fields $this->objSourceobject = $objSourceObjectTemp; foreach ($this->arrFields as $objOneField) { if ($objOneField->getObjSourceObject() != null) { $objOneField->setObjSourceObject($objSourceObjectTemp); } } } } return count($this->arrValidationErrors) == 0; }