dongzhukuai8177 2015-12-29 10:19
浏览 73
已采纳

单击按钮后如何更改字段ActiveForm中的值?

How change value 'userid' in field ActiveForm after click on button?

How it make?

I need to check field 'userid' and change value.

I not know how it make.


View:

_Form.php

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;

?>

<div class="account-form">

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

    <?= $form->field($model, 'userid')->textInput(['maxlength' => true])?>

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

    <?php ActiveForm::end(); ?>
</div>

Create.php

<div class="account-create">

    <h1><?= Html::encode($this->title) ?></h1>

    <?= $this->render('_form', ['model' => $model,]) ?>

</div>

Controller:

AccountController.php

class AccountController extends Controller
{

public function actionCreate()
    {
        $model = new Account();

        if ($model->load(Yii::$app->request->post() && $model->save())) {

         return $this->redirect(['view', 'id' => $model->userid]);
        } else {

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

}

Model:

Account.php

class Account extends \yii\db\ActiveRecord
{

    public function rules()
    {

        return [
            [['userid'], 'required']
        ];
    }

}
  • 写回答

1条回答 默认 最新

  • duanqun9618 2015-12-29 11:06
    关注

    Try this in your controller:

    public function actionCreate()
    {
        $model = new Account();
    
        if (isset($_POST['Account']){
    
            $model->attributes = $_POST['Account'];
            $model->userid = 123;
    
            /* in case you had errors
            // $model->validate();
            // var_dump($model->getErrors());
            */
    
            if($model->save()){
    
                return $this->redirect(['view', 'id' => $model->userid]);
            }
        }
        else{
    
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真