Yii2在自定义模板表单字段中读取已保存的数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Yii2在自定义模板表单字段中读取已保存的数据相关的知识,希望对你有一定的参考价值。
我为表单字段(yiiootstrapActiveForm
)创建了一个模板。
_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>'
])?>
它工作正常并将数据保存在数据库中。
但是当我尝试更新时,不会加载值。不检查radiobox。
如何创建模板并从数据库中读取保存的数据?
控制器动作:
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>
编辑
下面你可以找到我想在Yii中改造的template
optino里面的实际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>'
])?>
答案
好吧,没有你所遵循的实际HTML
,但你通过图像在问题中提到的特定布局,你仍然可以使用ActiveForm
$form->field()->radioList()
方法和template
选项实现它。
您可以生成类似于您给定布局的下面的内容
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>
以上是关于Yii2在自定义模板表单字段中读取已保存的数据的主要内容,如果未能解决你的问题,请参考以下文章
织梦CMS如何在自定义表单页调用arclist标签啊,怎么在自定义表单页调用无效!