【问题标题】:DepDrop in yii2 not work?yii2中的DepDrop不起作用?
【发布时间】:2018-08-21 09:24:17
【问题描述】:

我正在使用 DepDrop 作为依赖项下拉列表。为此,我编写了以下代码:

在视图中:

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']),
    ],
]);

控制器:

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' => '']);
}

但它不起作用。我发现在actionList函数的第一个包含跟随代码没有调用actionList。

file_put_contents("c:/testtest.txt", implode(',',$_POST));

这是请求的负责人:

请求网址:http://admin.same.ir/index.php?r=faculty/list

请求方法:POST

状态码:400 错误请求

远程地址:127.0.0.1:80

推荐人政策:no-referrer-when-downgrade

不知道是什么问题!!

【问题讨论】:

  • 可能不相关,但 "c:/testtest.txt" 应该是 "c:\testtest.txt" 因为 Windows。
  • 没有。我只是想说这个函数没有被调用。

标签: php yii2 dropdown


【解决方案1】:

这只是针对 CSRF: 我在控制器中添加了以下代码:

  public $enableCsrfValidation = false;

【讨论】:

  • 你永远不应该关闭enableCsrfValidation 这意味着你让你的网站容易受到跨站脚本的攻击
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多