Yii2在自定义模板表单字段中读取保存的数据

时间:2018-08-30 17:43:45

标签: php yii yii2

我为表单字段(yii\bootstrap\ActiveForm)创建了一个模板。

_form.php

<?php
echo $form->field($model, 'test_radio', [
    'template' =>
    '<div id="user-test_radio" value="" >                                    
                            <p style="margin:0"><label>TYPE</label></p>
                            <p style="margin:0"><label><input type="radio" name="user[test_radio]" value="PF" checked=""/> PF </label></p>
                            <p ><label><input type="radio" name="user[test_radio]" value="PJ" checked=""/> PJ </label></p>
                        </div>'
])?>

工作正常,并将数据保存在数据库中。

但是当我尝试更新时,未加载值。单选框未选中。

如何创建模板并从数据库中读取保存的数据?

控制器动作:

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

    if( $model->load(Yii::$app->request->post()) && $model->save() ){
        return $this->redirect(['view', 'id' => $model->id]);
    }

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

/**
 * Updates an existing User model.
 * If update is successful, the browser will be redirected to the 'view' page.
 * @param integer $id
 * @return mixed
 * @throws NotFoundHttpException if the model cannot be found
 */
public function actionUpdate($id) {
    $model = $this->findModel($id);

    if( $model->load(Yii::$app->request->post()) && $model->save() ){
        return $this->redirect(['view', 'id' => $model->id]);
    }

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

这是我要复制的HTML:

body {
  margin: 10px;
}
<link href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap-theme.min.css" rel="stylesheet" />
<div class="col-md-12">
  <legend>Demo</legend>


  <div class="col-md-12">

    <div class="row">
      <div class="col-md-6 ">

        <p>
        </p>
        <div class="form-group field-provider-demo_type">
          <div id="provider-demo_type" value="">
            <p style="margin:0"><label>TYPE</label></p>
            <p style="margin:0"><label><input type="radio" name="Provider[demo_type]" value="PF" checked=""> PF </label></p>
            <p><label><input type="radio" name="Provider[demo_type]" value="PJ" checked=""> PJ </label></p>
          </div>
        </div>
        <p></p>

        <p>
        </p>
        <div class="form-group field-provider-demo_default">
          <div id="provider-demo_default">
            <p style="margin:0"><label>DEFAULT</label></p>
            <p style="margin:0"><label><input type="radio" name="Provider[demo_default]" value="Acme" checked=""> Acme </label></p>
            <p><label><input type="radio" name="Provider[demo_default]" value="Custom" checked=""> Custom </label></p>
          </div>
        </div>
        <p></p>


      </div>



      <div class="col-md-6 ">

        <p>


        </p>
        <div>
          <p style="margin:0"><label><input type="radio" name="Provider[demo]" value="Own"> Own</label></p>
          <p><label><input type="radio" name="Provider[demo]" value="Service (freela)"> Service (freela)</label></p>
        </div>


        <p></p>

        <p>


        </p>
        <div>
          <p style="margin:0"><label>ASSIGNMENT</label></p>
          <p style="margin:0"><label><input type="radio" name="Provider[demo_assignment]" value="Total"> Total</label></p>
          <p style="margin:0"><label><input type="radio" name="Provider[demo_assignment]" value="Partial"> Partial</label></p>
          <p style="margin:0"><label><input type="radio" name="Provider[demo_assignment]" value="Open source"> Open source</label></p>
        </div>
        <p></p>

      </div>

    </div>

  </div>





  <p></p>

</div>

Something like this img


编辑

您可以在下面的template Optino中找到我要在Yii中进行转换的实际html内容

<?php
echo $form->field($model, 'test_radio', [
    'template' =>
    '<div id="job-category" >                                    
        <h2>Category</h2>
          <input type="radio" name="Job[category]" value="male"> Freelancer<br>
          <p><label>Art</label></p>
          <input type="radio" name="Job[category]" value="Diagrammer"> Diagrammer<br>
          <input type="radio" name="Job[category]" value="Illustrator"> Illustrator<br>
          <input type="radio" name="Job[category]" value="Cartographer"> Cartographer<br>
          <p><label>ICO</label></p>
          <input type="radio" name="Job[category]" value="Photographer"> Photographer<br>
          <input type="radio" name="Job[category]" value="Cartoonist"> Cartoonist<br>

          <p><label>TEXT</label></p>
          <input type="radio" name="Job[category]" value="Author"> Author<br>

          <p><label>LOT</label></p>
          <input type="radio" name="Job[category]" value="Image Bank"> Image Bank<br>
          <input type="radio" name="Job[category]" value="Literary Agency"> Literary Agency<br>
          <input type="radio" name="Job[category]" value="News Agency"> News Agency<br>

    </div>'
])?>

1 个答案:

答案 0 :(得分:1)

好吧,您没有遵循实际的HTML,而是通过图像在问题中提到的特定布局,您仍然可以使用ActiveForm $form->field()->radioList()实现它方法和template选项。

您可以生成类似于您的给定布局的以下内容

enter image description here

echo $form->field($model, 'type', [
    'template' =>'{label}<br/>{input}{error}'
])->radioList(['PF'=> 'PF', 'PJ' => 'PJ'],['tag'=>'div','separator'=>'<br/>']);

echo $form->field($model, 'default', [
    'template' =>'{label}<br/>{input}{error}'
])->radioList(['Acme'=> 'Acme', 'Custom' => "Custom",'Own'=>'Own'],['tag'=>'div','separator'=>'<br/>']);

以上内容将生成如下的HTML

<div class="form-group field-provider-type required">
  <label class="control-label">Type</label><br>
  <input type="hidden" name="Provider[type]" value="">
  <div id="provider-type" aria-required="true">
    <label><input type="radio" name="Provider[type]" value="PF" checked=""> PF</label><br>
    <label><input type="radio" name="Provider[type]" value="PJ"> PJ</label>
  </div>
  <div class="help-block"></div>
</div>