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

Sharing Knowledge Alumni Akakom, Donny Verdian, Lead UI/UX Consultant
Sharing Knowledge Alumni Akakom, Donny Verdian, Lead UI/UX Consultant

6 tahun yang lalu

Sabtu (06/10), Alumni STMIK AKAKOM Bapak Donny Verdian mengadakan acara sharing knowledge dengan tema UX dalam Development Software. Acara ini dikemas ...

Selengkapnya
3 Dosen Akakom mengikuti Konferensi Internasional ICIRSTM 2018 di National University Singapore
3 Dosen Akakom mengikuti Konferensi Internasional ICIRSTM 2018 di National University Singapore

6 tahun yang lalu

International Conference on Innovative Research in Science, Technology & Management (ICIRSTM) 2018 merupakan konferensi yang diadakan di UTown National University Singapore. ...

Selengkapnya
10 Mahasiswa Akakom mengikuti program internship di Authscure SDN BHD
10 Mahasiswa Akakom mengikuti program internship di Authscure SDN BHD

6 tahun yang lalu

AuthScure merupakan perusahaan yang berbasis di Kuala Lumpur, Malaysia. Perusahaan ini memiliki fokus layanan cyber security. Authscure juga memiliki engineer ...

Selengkapnya

Teknologi Populer