/** * Sets a model to the controller * * @param CoreModel $model The model */ public function setModel(CoreModel $model) { $this->{$model->getModelName()} = $model; }
/** * binds the model to the Form. * * @param coreModel $model the model to represent */ public function initFromModel(CoreModel $model) { $this->model = $model; $modelName = explode('Model', $model->getModelName()); $this->setAction('/?controller=' . $modelName[0] . '&action=save'); $this->setName($modelName[0]); $this->setHiddenFields($this->model->keys); $fields = $this->model->getFields(); $this->model->configure(); $configuredFields = $this->model->getTypes(); $validators = $this->model->getValidators(); $formFields = array_keys($this->getFields()); if (is_array($fields) && count($fields)) { foreach ($fields as $fieldName) { if (!in_array($fieldName, $this->hiddenFields)) { $type = isset($configuredFields[$fieldName]) ? $configuredFields[$fieldName]['type'] : 'text'; if (!in_array($fieldName, $formFields)) { $element = FormElementFactory::getElement($type); $element->setType($type); $element->setAttributes($configuredFields[$fieldName]['attributes']); $element->setName($fieldName); $element->setLabel($fieldName); $element->setValue($model->{$fieldName}); $this->setValidators($element, $validators); $this->setField($element); } } } } }