Berita

Mengubah form untuk Input Koleksi Buku pakai kelas CActiveForm

11 tahun yang lalu

(oleh: Badiyanto)

Form masukan secara otomatis terbentuk pada saat menggunakan CRUD genereator. Dalam kasus ini model/kelas TblBuku akan menempatkan modul input/ouput yang diletakkan  dalam forder protected/views/tblBuku/...  untuk itu buka dan ubah file _form.php seperti pada kode berikut :

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'tbl-buku-form',
    'enableAjaxValidation'=>false,
)); ?>
    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>

    <div class="row">
        <?php echo $form->labelEx($model,'judul_buku'); ?>
        <?php echo $form->textField($model,'judul_buku',array('size'=>60,'maxlength'=>100)); ?>
        <?php echo $form->error($model,'judul_buku'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'pengarang_id'); ?>
       
        <?php echo $form->dropDownList($model,'pengarang_id',CHtml::listData(
                TblPengarang::model()->findAll(), 'id','nama_pengarang'),
                array('prompt'=>'= Pilihan =','style'=>'width:200px;')); ?>
        <?php echo $form->error($model,'pengarang_id'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'penerbit_id'); ?> 
        <?php echo $form->dropDownList($model,'penerbit_id',
                   CHtml::listData(TblPenerbit::model()->findAll(), 'id','nama_penerbit'),
              array('prompt'=>'= Pilihan =','style'=>'width:200px;')); ?>
        <?php echo $form->error($model,'penerbit_id'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'isbn_issn'); ?>
        <?php echo $form->textField($model,'isbn_issn',array('size'=>30,'maxlength'=>30)); ?>
        <?php echo $form->error($model,'isbn_issn'); ?>
    </div>

    <div class="row buttons">
        <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
    </div>

<?php $this->endWidget(); ?>

</div><!-- form -->

 

Perintah :
<?php echo $form->dropDownList($model,'penerbit_id',
          CHtml::listData(TblPenerbit::model()->findAll(), 'id','nama_penerbit'),
         array('prompt'=>'= Pilihan =','style'=>'width:200px;')); ?>

adalah menyajikan input dengan ComboBox/dropDownList  dengan isi pilihan nama pengarang mengambil dari model
TblPenerbit::model()->findAll(), 'id','nama_penerbit'

Kemudian bagain create.php

<?php
/* @var $this TblBukuController */
/* @var $model TblBuku */

$this->breadcrumbs=array(
    'Tbl Bukus'=>array('index'),
    'Create',
);

$this->menu=array(
    array('label'=>'List Buku', 'url'=>array('index')),
    array('label'=>'Manajemen Buku', 'url'=>array('admin')),
);
?>

<h1>Input Koleksi Buku</h1>
<?php echo $this->renderPartial('_form', array('model'=>$model)); ?>

penjelasan:

Tanpilan view craete.php ditambahkan form input koleksi buku dengan perintah :


<?php echo $this->renderPartial('_form', array('model'=>$model)); ?>


array('model'=>$model) : memangil _form.php dengan menyertakan variabel $model;


Hubungannya dengan TblBukuController.php

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

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['TblBuku']))
        {
            $model->attributes=$_POST['TblBuku'];
            if($model->save())
                $this->redirect(array('view','id'=>$model->id));
        }

        $this->render('create',array(
            'model'=>$model,
        ));
    }



Berita Lainnya

IEEE Conference ISRITI 2018 - Opportunities and Trends on The Decentralized Systems and Future of the Internet
IEEE Conference ISRITI 2018 - Opportunities and Trends on The Decentralized Systems and Future of the Internet

6 tahun yang lalu

Tahun ini, STMIK AKAKOM Yogyakarta telah menyelenggarakan Seminar 2018 International Seminar on Research of Information Technology and Intelligent Systems (ISRITI). Seminar ...

Selengkapnya
Yudistiro, lulusan tercepat kelas dicoding Kotlin Android Developer Expert
Yudistiro, lulusan tercepat kelas dicoding Kotlin Android Developer Expert

6 tahun yang lalu

Kelas Kotlin Android Developer Expert adalah kelas online yang diadakan oleh dicoding. Dicoding merupakan online learning platform yang memiliki misi ...

Selengkapnya
Dosen Akakom menjadi delegasi Indonesia mengikuti seminar, workshop dan kunjungan industri di China
Dosen Akakom menjadi delegasi Indonesia mengikuti seminar, workshop dan kunjungan industri di China

6 tahun yang lalu

Kegiatan 2018 Seminar on Promoting Information Connectivity for Asian Countries didanai oleh China Aid dan diorganisasi oleh International Cooperation Center, National ...

Selengkapnya

Teknologi Populer