There is a database field called path
it is text type and it is used to store paths marking strings as CSV, something like the following:
*:*,site/about,site/*
Originally, it is used as a text input field like the following:
<?= $form->field($model, 'path')->textarea(['rows' => 6]) ?>
I want to, temporary, convert those paths into a dropDownSelect list in the view, then in the controller I will implode the selected items into string again to be validated and saved.
In the view _form.php
I have the following:
<?= $form->field($model, 'path')->dropDownList($model->getPathSelectList($model->path), ['multiple' => 'multiple']) ?>
In the controller:
public function actionCreate()
{
$model = new StaticHtml();
// var_dump($model->path);
// die();
if (is_array($model->path)) $model->path = implode(',', $model->path);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => $model->id]);
} else {
$model->path = $model->getPathsList(Yii::$app->getRequest()->getQueryParam('path'));
return $this->render('create', [
'model' => $model,
]);
}
}
Please Notice the two commented lines var_dump($model->path)
and die()
I set them to debug, because every time I select from the dropDownList and submit the form, the validation fails and tell me that Path must be a string
and of course there is no any selection reserved unlike other fields and the var_dump($model->path)
prints NULL
:
In the code above, there is two model's methods:
getPathsList()
it is called from the controller and it convert an initial path to path marks and set them as csv string.getPathSelectList
in the view and it convert the csv into an associative array which its keys and values are equals for each one to be data list for the dropDownList field.
Till this point, I think it will need more for update, but however, What's the problem in my code that prevents posting the $model->path
so it prints NULL
with var_dump()
?
I'm pretty sure that it is a problem in receiving data from the form because I tried to:
var_dump(Yii::$app->getRequest()->post('path'));
var_dump(Yii::$app->getRequest()->post('title'));
var_dump(Yii::$app->request->post('path'));
and all of them returns NULL