在 Yii2 下提交后无法从控制器中的 ActiveForm 的文本输入中获取值

Can't get value from textinput of ActiveForm in controller after submit under Yii2

我制作了一个自定义模型,它是一个扩展 ModelUpload Form。我根据 yii2 关于 UploadedFile 的文档制作了一份副本,但到目前为止,正在工作的是保存在 web/uploads 下的上传文件。我制作了一个名为 $storeList 的 public 属性。我期待在我调用 $model->load((Yii::$app->request->post()) 之后,表单内的值将被分配给 $model->storeList 但变量只是空的。有人可以告诉我我哪里做错了吗?

结构是这样的。 index.php 首先加载,它包括 select_package.php,它有一个呈现 upload.php.

的选项卡

index.php:

<div id="uploadStatusFrame" class="uploadStatusFrame">
    <?php
        include('layouts/select_package.php');
    ?>
</div>

<button id="uploadButton" class="btn btn-success" 
                style='position:relative; left:10px; top: -44px'>Upload to Server</button>

<script>
$('#uploadButton').click(function(){
    $("#submitButton").trigger("click");
});
</script>

select_package.php:

<?= 
    Tabs::widget([
        'id' => 'dashboardTab',
        'options' => ['id' => 'mainTab',
                    'class' => 'nav nav-tabs',
                        'style' => 'font-size: 14px; font-weight: bold;border-bottom: none;',
                    ],
        'items' => [
        [
            'label' => 'New Firmware',
            'active' => true,
                'style' => 'font-size: 14px; font-weight: bold',
            'options' => ['id' => 'tab1',
                        'class' => 'nav nav-tabs',
                        'style' => 'width:1044px;margin:1px;background:#428bca; 
                        height:453px;border:2px solid #428bca;'
                        ],
            'content' => $this->render('/upload-package/upload', [
                            'model' => $model,
                        ]),
        ],
    ]);
?>

UploadPackageController.php:

class UploadPackageController extends Controller
{
    public $layout = "default";

    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all SystemConfig models.
     * @return mixed
     */
    public function actionIndex()
    {
        $model = new UploadForm;

        if ($model->load(Yii::$app->request->post())) {
            $model->imageFile = UploadedFile::getInstance($model, 'imageFile');
            $model->storeList = UploadedFile::getInstance($model, 'storeList');     // **<<-- How should I handle this?**

            if ($model->upload()){
                return $this->render('index', [
                    'model' => $model,
                    'storeList' => $model->storeList,
                ]);
            }
        }

        return $this->render('index', [
            'model' => $model,
            'storeList' => 'NONE',
        ]);
    }
}

UploadForm.php:

class UploadForm extends Model
{
    /**
     * @var UploadedFile
     */
    public $imageFile;
    public $storeList;

    public function rules()
    {
        return [

        ];
    }

    public function upload()
    {
        if ($this->validate()) {
            $this->imageFile->saveAs('uploads/' . $this->imageFile->baseName . '.' . $this->imageFile->extension);
            return true;
        } else {
            return false;
        }
    }
}

upload.php

    <?= $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data',
            'id' => 'fileSenderForm', 
            'name' => 'fileSenderForm', 
        ]]) ?>

    <?= $form->field($model, 'imageFile')->fileInput(); ?>

    <?= $form->field($model, 'storeList')->textInput(['value' => 'ANY_VALUE_HERE'])->label(); ?>

    <?= Html::submitButton('Submit', ['id' => 'submitButton']); ?>

    <?php ActiveForm::end() ?>

您的问题及其原因

如果您查看请求的内容,您会看到您的值已发送但未分配到模型中。这样做的原因是 Yii2 的安全特性。 load() 方法所做的称为模型属性的大量分配。因为这可能有潜在的危险,Yii 要求所有属性被批量分配为

  • 已验证
  • 或声明为'safe'(实际上也是验证器)

解决方案

所以...您需要将要通过 $model->load(Yii::$app->request->post()) 大量分配的属性添加到验证器。否则它们将被跳过。同时删除此字段的 UploadedFile::getInstance,因为此属性显然与文件无关。

您可以使用实际的验证器,也可以只声明它们是安全的。您的规则方法可能如下所示:

public function rules() {
    return [
        [['storeList'], 'string', 'max'=>64],
    ];
}

或者如果您不想指定任何 'real' 验证器,只需将属性声明为安全的:

public function rules() {
    return [
        [['storeList'], 'safe'],
    ];
}

了解这个

您可以在此处的官方指南中阅读有关此内容的所有详细信息:

http://www.yiiframework.com/doc-2.0/guide-structure-models.html#massive-assignment