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

单击按钮后如何更改字段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']
        ];
    }

}

图片转代码服务由CSDN问答提供 功能建议

点击按钮后如何在字段ActiveForm中更改值'userid'

如何制作?

我需要检查字段'userid'并更改值。

我不知道它是怎么做的。


查看:

_Form。 php

 &lt;?php 
 
use yii \ helpers \ Html; 
use yii \ widgets \ ActiveForm; 
 
?&gt; 
 
&lt; div  class =“account-form”&gt; 
 
&lt;?php $ form = ActiveForm :: begin();  ?&gt; 
 
&lt;?= $ form-&gt;字段($ model,'userid') - &gt; textInput(['maxlength'=&gt; true])?&gt; 
 
&lt; div  class =“form-group”&gt; 
&lt;?= Html :: submitButton($ model-&gt; isNewRecord?'Create':'Update',['class'=&gt; $ model-&gt; isNewRecord?'  btn btn-warning':'btn btn-primary'])?&gt; 
&lt; / div&gt; 
 
&lt;?php ActiveForm :: end();  ?&gt; 
&lt; / div&gt; 
   
 
 

Create.php

 &lt; div class =“account  -create“&gt; 
 
&lt; h1&gt;&lt;?= Html :: encode($ this-&gt; title)?&gt;&lt; / h1&gt; 
 
&lt;?= $ this-&gt; 渲染('_ form',['model'=&gt; $ model,])?&gt; 
 
&lt; / div&gt; 
   
 
 

控制器:

AccountController.php

 类AccountController扩展Controller 
 {
 \  npublic function actionCreate()
 {
 $ model = new Account(); 
 
 if if($ model-&gt; load(Yii :: $ app-&gt; request-&gt; post()&amp;&amp;  $ model-&gt; save())){
 
返回$ this-&gt; redirect(['view','id'=&gt; $ model-&gt; userid]); 
} else {
  
返回$ this-&gt; render('create',[
'model'=&gt; $ model,
]); 
} 
} 
 
} 
   
 
 

<强>型号:

Account.php

  类帐户extends \ yii \ db \ ActiveRecord 
 {
 
公共函数规则()
 {
 
  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,
            ]);
        }
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题