I'm using DepDrop for dependency Dropdownlist. for this, I've written follow codes:
in view form:
use kartik\depdrop\DepDrop;
$catList = ['1' => 1, '2' => 2];
echo $form->field($model, 'cat')->dropDownList($catList, ['id' => 'cat-id']);
// Child # 1
echo $form->field($model, 'subcat')->widget(DepDrop::classname(), [
'options' => ['id' => 'subcat-id'],
'pluginOptions' => [
'depends' => ['cat-id'],
'placeholder' => 'Select...',
'url' => \yii\helpers\Url::to(['/faculty/list']),
],
]);
Controller:
public function actionList()
{
$out = [];
if (isset($_POST['depdrop_parents'])) {
$parents = $_POST['depdrop_parents'];
if ($parents != null) {
$cat_id = $parents[0];
$out = [
['id' => '<sub-cat-id-1>', 'name' => '<sub-cat-name1>'],
['id' => '<sub-cat_id_2>', 'name' => '<sub-cat-name2>'],
];
echo Json::encode(['output' => $out, 'selected' => '']);
return;
}
}
echo Json::encode(['output' => '', 'selected' => '']);
}
but it doesn't work. I found out that actionList is't called by include follow code at the first of actionList function.
file_put_contents("c:/testtest.txt", implode(',',$_POST));
and this is head of request:
Request URL: http://admin.same.ir/index.php?r=faculty/list
Request Method: POST
Status Code: 400 Bad Request
Remote Address: 127.0.0.1:80
Referrer Policy: no-referrer-when-downgrade
I don't know what is the problem!!
it was just for CSRF: I added follow code to controller:
public $enableCsrfValidation = false;