2016-09-12 3 views
0

Ich habe versucht, Formulareingabe mit Auto-Nummer mit Erweiterungen mdmsoft/yii2-autonumber zu machen. Und das Ergebnis in meinem Browser unten. enter image description here Und das ist meine Codeform im Blick.Formulareingabe mit Auto-Nummer yii2 etwas falsch

<?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 
 
\t \t echo "&nbsp"; 
 
\t \t echo "&nbsp"; 
 
\t \t echo Html::a('Keluar', ['index'],['class'=>'btn btn-primary']); 
 
\t \t ?> 
 
    </div> 
 

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

 
</div>

Und ich Autowert Anzeige in Textfeld "Kode DoNatur" wollen. Ein Beispiel ist im Bild unten zu sehen. enter image description here

-Code in Controller

<?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.'); 
 
     } 
 
    } 
 

 
}

-Code in Modell

<?php 
 

 
namespace app\models; 
 

 
use yii\db\ActiveRecord; 
 

 
class Donatur extends ActiveRecord 
 
{ 
 
\t public static function tableName() 
 
\t { 
 
\t \t return 'donatur'; 
 
\t } 
 

 
\t public function rules() 
 
\t { 
 
\t \t return [ 
 
\t \t  [['nama_donatur', 'alamat', 'telepon'], 'required'], 
 
\t \t  [['kode_donatur', 'nama_donatur', 'alamat'], 'string'], 
 
\t \t  [['telepon'], 'integer'], 
 
\t \t  ]; 
 
\t } 
 

 
\t 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. 
 
     ], 
 
    ]; 
 
    } 
 
\t 
 
}

+0

Wo Sie Auto-Nummer generieren? .show Code –

+0

Ihren entsprechenden Aktionscode anzeigen .. bitte – scaisEdge

Antwort

0

Ihr Verhalten() Funktion ist in dem Modell nicht diese in Ihrem controller.Like sein sollte,

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. 
     ], 
    ]; 
}