public function actionEditField() { $id = (int) Yii::$app->request->get('id'); // Get Base Field $field = ProfileField::findOne(['id' => $id]); if ($field == null) { $field = new ProfileField(); } // Get all Available Field Class Instances, also bind current profilefield to the type $profileFieldTypes = new BaseType(); $fieldTypes = $profileFieldTypes->getTypeInstances($field); // Build Form Definition $definition = array(); $definition['elements'] = array(); // Add all sub forms $definition['elements'] = array_merge($definition['elements'], $field->getFormDefinition()); foreach ($fieldTypes as $fieldType) { $definition['elements'] = array_merge($definition['elements'], $fieldType->getFormDefinition()); } // Add Form Buttons $definition['buttons'] = array('save' => array('type' => 'submit', 'label' => Yii::t('AdminModule.controllers_UserprofileController', 'Save'), 'class' => 'btn btn-primary')); if (!$field->isNewRecord && !$field->is_system) { $definition['buttons']['delete'] = array('type' => 'submit', 'label' => Yii::t('AdminModule.controllers_UserprofileController', 'Delete'), 'class' => 'btn btn-danger pull-right'); } // Create Form Instance $form = new HForm($definition); // Add used models to the CForm, so we can validate it $form->models['ProfileField'] = $field; foreach ($fieldTypes as $fieldType) { $form->models[get_class($fieldType)] = $fieldType; } // Form Submitted? if ($form->submitted('save') && $form->validate()) { // Use ProfileField Instance from Form with new Values $field = $form->models['ProfileField']; $fieldType = $form->models[$field->field_type_class]; if ($field->save() && $fieldType->save()) { return $this->redirect(Url::to(['/admin/user-profile'])); } } if ($form->submitted('delete')) { $field->delete(); return $this->redirect(Url::to(['/admin/user-profile'])); } return $this->render('editField', array('hForm' => $form, 'field' => $field)); }
/** * Returns the Field Rules, to validate users input * * @param type $rules * @return type */ public function getFieldRules($rules = array()) { $rules[] = array($this->profileField->internal_name, 'integer'); if ($this->maxValue) { $rules[] = array($this->profileField->internal_name, 'integer', 'max' => $this->maxValue); } if ($this->minValue) { $rules[] = array($this->profileField->internal_name, 'integer', 'min' => $this->minValue); } return parent::getFieldRules($rules); }
/** * Validator which checks the fieldtype * * Also ensures that field_type_class could not be changed on existing records. */ public function checkType() { if (!$this->isNewRecord) { // Dont allow changes of internal_name - Maybe not the best way to check it. $currentProfileField = ProfileField::findOne(['id' => $this->id]); if ($this->field_type_class != $currentProfileField->field_type_class) { $this->addError('field_type_class', Yii::t('UserModule.models_ProfileField', 'Field Type could not be changed!')); } } else { $profileFieldTypes = new fieldtype\BaseType(); if (!key_exists($this->field_type_class, $profileFieldTypes->getFieldTypes())) { $this->addError('field_type_class', Yii::t('UserModule.models_ProfileField', 'Invalid field type!')); } } }
/** * Returns the Field Rules, to validate users input * * @param type $rules * @return type */ public function getFieldRules($rules = array()) { $rules[] = array($this->profileField->internal_name, DbDateValidator::className(), 'format' => Yii::$app->params['formatter']['defaultDateFormat']); return parent::getFieldRules($rules); }
/** * Returns the Field Rules, to validate users input * * @param type $rules * @return type */ public function getFieldRules($rules = array()) { if ($this->validator == self::VALIDATOR_EMAIL) { $rules[] = array($this->profileField->internal_name, 'email'); } elseif ($this->validator == self::VALIDATOR_URL) { $rules[] = array($this->profileField->internal_name, 'url'); } if ($this->maxLength == "" || $this->maxLength > 255) { $rules[] = array($this->profileField->internal_name, 'string', 'max' => 255); } else { $rules[] = array($this->profileField->internal_name, 'string', 'max' => $this->maxLength); } if ($this->minLength != "") { $rules[] = array($this->profileField->internal_name, 'string', 'min' => $this->minLength); } if ($this->regexp != "") { $errorMsg = $this->regexpErrorMessage; if ($errorMsg == "") { $errorMsg = "Invalid!"; } $rules[] = array($this->profileField->internal_name, 'match', 'pattern' => $this->regexp, 'message' => $errorMsg); } return parent::getFieldRules($rules); }
/** * Returns the Field Rules, to validate users input * * @param type $rules * @return type */ public function getFieldRules($rules = array()) { $rules[] = array($this->profileField->internal_name, 'safe'); return parent::getFieldRules($rules); }
/** * Returns the Field Rules, to validate users input * * @param type $rules * @return type */ public function getFieldRules($rules = array()) { $rules[] = array($this->profileField->internal_name, 'date', 'timestampAttribute' => $this->profileField->internal_name); return parent::getFieldRules($rules); }
/** * Returns the Field Rules, to validate users input * * @param type $rules * @return type */ public function getFieldRules($rules = array()) { $rules[] = array($this->profileField->internal_name, 'in', 'range' => array_keys($this->getSelectItems())); return parent::getFieldRules($rules); }
/** * @inheritdoc */ public function getFieldRules($rules = array()) { $rules[] = [$this->profileField->internal_name, DbDateValidator::className(), 'format' => Yii::$app->formatter->dateInputFormat, 'convertToFormat' => 'Y-m-d']; return parent::getFieldRules($rules); }
/** * @inheritdoc */ public function getFormDefinition($definition = array()) { return count($definition) > 0 ? parent::getFormDefinition($definition) : []; }
/** * Returns the Field Rules, to validate users input * * @param type $rules * @return type */ public function getFieldRules($rules = array()) { $rules[] = array($this->profileField->internal_name, DbDateValidator::className(), 'format' => self::DATEPICKER_FORMAT); return parent::getFieldRules($rules); }