Hi, I’m new to Yii and I would like some help.
I’m working on a project in which there exist a back office allowing an admin to add a new field for the users to enter informations. I need to create a field type checkboxlist and to push it to the database.
Here are:
- the columns of the table profile field allowing an admin user to add a field for profiles
- what I want the user to be able to select :
- how it is stocked for a user (in the table profile_extra):
- an example of a value stocked for a user’s birthdate.
The field_id from profile_extra is the id of profile field.
Well now need the value column to stock a checkboxlist result from a form.
Knowing that each field type has a model, I created a MultiCheckbox model to be able to add a field of this type from the back office :
<?php
…
class MultiCheckbox extends BaseType
{
public $options;
public function rules()
{
return [
[['options'], 'safe'],
];
}
// Allows the admin to enter his checkbox options in the backoffice dashboard
public function getFieldOptions()
{
return array_merge(parent::getFieldOptions(), [
'options' => [
'type' => 'textarea',
'label' => Yii::t('app', 'Values'),
'hint' => Yii::t('app', 'Every {0} on each line', '<code>value => title</code>'),
],
]);
}
public function getFieldRules()
{
$rules = [];
$rules[] = [$this->profileField->alias, 'default', 'value' => $this->profileField];
return array_merge(parent::getFieldRules(), $rules);
}
public function getFieldInput($form, $model, $options = [])
{
return $form->field($model, $this->profileField->alias)->checkboxList($this->getSelectItems(), [
'class' => 'form-group field-' . $this->profileField->category->alias . '-' . $this->profileField->alias . '',
]);
}
// returns the options so they are formatted
public function getSelectItems()
{
$items = [];
foreach (explode("\n", $this->options) as $option) {
if (strpos($option, '=>') !== false) {
list($key, $value) = explode('=>', $option);
$items[trim($key)] = Yii::t($this->profileField->language_category, trim($value));
} else {
$items[] = Yii::t($this->profileField->language_category, trim($option));
}
}
return $items;
}
public function formatValue($value, $raw = false)
{
if ($raw) {
return $value;
}
$items = $this->getSelectItems();
return Html::encode(isset($items[$value]) ? $items[$value] : $value);
}
}
Now all the other field types are working, even one that I created that uses a Date form (widget DatePicker). But my checkboxList is not pushed to the database after submit.
The class SettingsController performs the post functions with ajaxValidations.
Here is the function that do it for the extra fields :
public function actionExtraFields()
{
$categoryAlias = Yii::$app->request->post('categoryAlias');
$profileFields = ProfileField::getFields($categoryAlias);
$profileExtra = ProfileExtra::getExtraFields(Yii::$app->user->id);
$model = ProfileExtraForm::createFromFields(
ArrayHelper::getValue($profileFields, $categoryAlias, []),
ArrayHelper::getValue($profileExtra, $categoryAlias, []),
$categoryAlias
);
$this->performAjaxValidation($model);
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
foreach ($model->getAttributes() as $attribute => $value) {
ProfileExtra::saveValue(Yii::$app->user->id, $categoryAlias, $attribute, $value);
}
}
Yii::$app->session->setFlash(
'success_' . $categoryAlias,
Yii::t('app', 'Your profile has been updated')
);
return $this->redirect(['profile', '#' => $categoryAlias]);
}
My question is : How should I send checkboxList informations to the database with that specific configuration of classes ?
I understand that it may not be very clear for you, so ask me questions if needed.