Yii2 向下拉列表中添加新值

Yii2 向下拉列表中添加新值,yii2,select2,active-form,Yii2,Select2,Active Form,在项目/创建活动表单中,我有一个与字段相关的公司帐户作为下拉列表。在该字段后面,我想用加号或其他符号将新帐户添加到下拉列表中,其行为如下: 收集迄今为止完成的所有输入,如$input=COMPACTARY\u keysget\u defined\u VAR;但在客户端可能需要 跳转到帐户/创建并传递$input 提交新帐户后,跳回项目/创建,例如返回$this->redirectYii::$app->request->referer;并填写之前输入的数据extract$input,Extra_P

在项目/创建活动表单中,我有一个与字段相关的公司帐户作为下拉列表。在该字段后面,我想用加号或其他符号将新帐户添加到下拉列表中,其行为如下:

收集迄今为止完成的所有输入,如$input=COMPACTARY\u keysget\u defined\u VAR;但在客户端可能需要 跳转到帐户/创建并传递$input 提交新帐户后,跳回项目/创建,例如返回$this->redirectYii::$app->request->referer;并填写之前输入的数据extract$input,Extra_PREFIX_SAME,arr; 我现在正努力解决几个问题:

这个过程是否符合最佳实践,或者我应该从根本上改变什么? 这个按钮怎么样?提交按钮、链接还是某种形式的javascript? “提交”按钮的问题在于,并非所有必填字段都可以填写。因此,可能无法保存和恢复/更新项目模型。 链接的问题在于它是在输入数据之前构建的 javascript的问题是我没有胶水
欢迎任何提示。提前谢谢。

我建议的另一种选择是使用

至于AddAccounts按钮,我将使用Submit按钮,并为实际的Submit按钮赋予不同的名称。表单中的Submit按钮有两个,如中所述。因此,“项目/创建”视图将如下所示:

<?php $form = ActiveForm::begin(); ?>

...
...
...

<?= $form->field($model, 'account_id')->widget(Select2::classname(), [
    'data' => ArrayHelper::map(Account::find()->all(), "id", "name"),
    'options' => ['placeholder' => 'Select a related company account ...'],
    'pluginOptions' => [
        'allowClear' => true
    ],
]) ?>

<?= Html::submitButton('Add Account ;)', ['class' => 'btn btn-success', 'name' => 'add_account_submit']) ?>

...
...
...

<div class="form-group">
    <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
</div>

<?php ActiveForm::end(); ?>
项目模型如下所示:

class ProjectsController extends Controller
{

...
...
...

    public function actionCreate($category)
    {
        $model = new Projects();

        if (Projects::isSavedInSession()) {
            $model->loadFromSession();
        }
        if (Yii::$app->request->post('add_account_submit')) { // if add_account_submit is clicked
            $model->saveTosession(Yii::$app->request->post('Projects')); // I assume your model named Projects, if not, change this value to your model name
            return $this->redirect(['accounts/create']);
        }
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            $model->clearSession(); // we dont need the session anymore
            return $this->redirect(['index');
        }
        return $this->render('create', [
            'model' => $model,
        ]);
    }

...
...
...
}
class Projects extends \yii\db\ActiveRecord
{
...
...
...

    public static function isSavedInSession() { // why this is static is beyond this question context
        if (Yii::$app->session->get('projects')) return true;
        return false;
    }

    public function loadFromSession() {
        if (Yii::$app->session->get('projects_name')) $this->name = if (Yii::$app->session->get('projects_name'));
        if (Yii::$app->session->get('projects_account_id')) $this->account_id = if (Yii::$app->session->get('projects_account_id'));
        ...
        ... // insert all model's field here
        ...
    }

    public function saveToSession($fields) {
        Yii::$app->session->set('projects', 1);
        foreach ($fields as $field=>$value) {
            Yii::$app->session->set('projects_' . $field, $value);
        }
    }

    public function clearSession() {
        Yii::$app->session->remove('projects'));
        Yii::$app->session->remove('projects_name'));
        Yii::$app->session->remove('projects_account_id'));
        ...
        ... // insert all model's field here
        ...
    }

...
...
...
}
在AccountsController中,如果设置了projects会话,只需告诉程序跳回projects/create,如下所示:

class AccountsController extends Controller
{

...
...
...

    public function actionCreate($category)
    {
        $model = new Accounts();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            if (Projects::isSavedInSession()) {
                return $this->redirect(['projects/create');
            }
            return $this->redirect(['index');
        }
        return $this->render('create', [
            'model' => $model,
        ]);
    }

...
...
...
}
看起来有点长,但值得一试。无论如何,您可以将此方法用于另一个目的,例如保存当前表单状态

哦,还有一件事,我还没有在真正的代码中测试过,所以如果我的代码中出现任何错误,请在注释中联系我


快乐编码:

如果我是你,我会使用一个模态形式,当你按下加号按钮时会出现。表单需要使用ajax来发送要保存的数据,您可能希望使用带有Select2的ajax加载,这样您就不必担心添加新选项了。听起来是个不错的选择。我一定会试试看。谢谢,很好,谢谢。它解决了跳开和跳回的问题,而不需要验证。与marche的想法相比,它不需要AJAX,并且保持在同一层/没有弹出窗口。