/** * Return messages generated by form field validators * * @return array */ public function getMessages() { if ($this->_formCreated === false) { throw new \Engine\Exception('Form not created!'); } return $this->_form->getMessages(); }
/** * Save form data to database * * @params array $data * @return integer|bool */ public final function save(array $data = [], $validate = true) { if (!$this->isFormCreated()) { throw new \Engine\Exception('Form not init!'); } if (empty($data)) { $data = $this->getData(); } if ($validate) { if (!$this->isValid($data)) { $messages = []; foreach ($this->_form->getMessages() as $message) { /*$result = []; $result[] = "Message: ".$message->getMessage(); $result[] = "Field: ".$message->getField(); $result[] = "Type: ".$message->getType(); $messages[] = implode (", ", $result);*/ $messages[] = $message->getMessage(); } return ['error' => $messages]; } } $this->_preSave(); $data = []; $saveData = []; $alter = []; foreach ($this->_fields as $key => $field) { $d = $field->getSaveData(); if (!$d) { $alter[] = $key; continue; } /*if ($field instanceof Field\TranslationText) { if (!isset($saveData['translations'])) { $saveData['translations'] = []; } $saveData['translations'][$d['data']['key']] = $d['data']['value']; } else {*/ $saveData[$d['key']] = $d['value']; //} } $saveData = array_merge($this->_addData, $saveData); if (null !== $this->_id) { $result = $this->_update($this->_id, $saveData); if (is_array($result)) { return $result; } $this->_isInsertData = false; $this->_isUpdateData = true; } else { $result = $this->_insert($saveData); if (is_array($result)) { return $result; } $this->_isUpdateData = false; $this->_isInsertData = true; $this->loadData($result); } $this->_postSave(); return $this->_id; }