我尝试使用扩展名mdmsoft / yii2-autonumber使用自动编号进行表单输入。而这个结果在我的浏览器中如此吼叫。 这是我的代码形式。
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model app\models\Donatur */
/* @var $form yii\widgets\ActiveForm */
?>
<div class="donatur-form">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($model, 'kode_donatur')->textInput(['readonly' => true, 'value' => 'kode_donatur']) ?>
<?= $form->field($model, 'nama_donatur')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'alamat')->textArea(['rows' => 6]) ?>
<?= $form->field($model, 'telepon')->textInput(['maxlength' => true]) ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Simpan' : 'Ubah', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
<?php
echo " ";
echo " ";
echo Html::a('Keluar', ['index'],['class'=>'btn btn-primary']);
?>
</div>
<?php ActiveForm::end(); ?>
</div>
我希望在文本字段“Kode donatur”中显示自动编号。在下图中可以看到一个例子。
控制器中的代码
<?php
namespace app\controllers;
use Yii;
use app\models\Donatur;
use app\models\SearchDonatur;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
* DonaturController implements the CRUD actions for Donatur model.
*/
class DonaturController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
/**
* Lists all Donatur models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new SearchDonatur();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
]);
}
/**
* Displays a single Donatur model.
* @param string $id
* @return mixed
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Donatur model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
*/
public function actionCreate()
{
$model = new Donatur();
if (Yii::$app->request->post()) {
$model->load(Yii::$app->request->post());
if ($model->save()) {
\Yii::$app->session->setFlash('success', 'Data berhasil disimpan!');
} else {
Yii::$app->session->setFlash('error', 'Data gagal disimpan!');
}
return $this->redirect(['index']);
return $this->refresh();
}
else {
return $this->render('create', ['model' => $model]);
}
}
/**
* Updates an existing Donatur model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param string $id
* @return mixed
*/
public function actionUpdate($id)
{
$model = Donatur::findOne($id);
if (Yii::$app->request->post()) {
$model->load(Yii::$app->request->post());
if ($model->save()) {
Yii::$app->session->setFlash('success', 'Data berhasil diupdate!');
} else {
Yii::$app->session->setFlash('error', 'Data gagal diupdate!');
}
return $this->redirect(['index']);
return $this->refresh();
}
else {
return $this->render('update', ['model' => $model]);
}
}
/**
* Deletes an existing Donatur model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param string $id
* @return mixed
*/
public function actionDelete($kode_donatur)
{
$model = Donatur::findOne($kode_donatur);
$model->delete();
Yii::$app->session->setFlash('delete', 'Data berhasil dihapus!');
return $this->redirect(['index']);
}
/**
* Finds the Donatur model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param string $id
* @return Donatur the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Donatur::findOne($id)) !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
}
模型中的代码
<?php
namespace app\models;
use yii\db\ActiveRecord;
class Donatur extends ActiveRecord
{
public static function tableName()
{
return 'donatur';
}
public function rules()
{
return [
[['nama_donatur', 'alamat', 'telepon'], 'required'],
[['kode_donatur', 'nama_donatur', 'alamat'], 'string'],
[['telepon'], 'integer'],
];
}
public function behaviors()
{
return [
[
'class' => 'mdm\autonumber\Behavior',
'attribute' => 'kode_donatur', // required
//'group' => $this->id_branch, // optional
'value' => 'D'.'?' , // format auto number. '?' will be replaced with generated number
'digit' => 4 // optional, default to null.
],
];
}
}
答案 0 :(得分:0)
你的behavior()函数不在它应该在你的控制器中的模型中。就像这样,
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
[
'class' => 'mdm\autonumber\Behavior',
'attribute' => 'kode_donatur', // required
//'group' => $this->id_branch, // optional
'value' => 'D'.'?' , // format auto number. '?' will be replaced with generated number
'digit' => 4 // optional, default to null.
],
];
}