[YII] Basic Form


Deskripsi
     Form digunakan sebagai interaksi antara user dan system, interaksi berupa inputan yang digunakan untuk berbagai macam keperluan, seperti register , login , penyimpanan data , etc.

Howto

in CONTROLLER on action Formulir :

     public function actionFormulir()
{
1 $model = new Yiitest;
2
3 if(isset($_POST['Yiitest']))
4 {
5 $model->name = $_POST['Yiitest']['name'];
6 $model->description = $_POST['Yiitest']['description'];
7 if ($model->save())
8 {
9 echo 'Berhasil disimpan';
10 }
11 else
12 {
13 echo 'Save Failure'.'<br>';
14 $error = $DbAr->getErrors();
15 echo $error['name']['0'];
16 }
17 }
18 else
19 {
20 $this->render('formulir',array('model'=>$model));
21 }
22 }

* why line 3 not true ? because the form is not recognize and the variable sent method post doesn't exist so the first execute is line 20

* look at number line 20 , we passing object model from CONTROLLER to VIEW, now we look view


in VIEW Formulir, you can learn this  :
<div class="form">
<?php $form=$this->beginWidget('CActiveForm'); ?>

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

<div class="row">
        <?php echo $form->label($model,'name'); ?>
        <?php echo $form->textField($model,'name') ?>
    </div>

<div class="row">
        <?php echo $form->label($model,'Deskripsi'); ?>
        <?php echo $form->textField($model,'description') ?>
    </div>

    <div class="row submit">
        <?php echo CHtml::submitButton('Simpan'); ?>
    </div>

<?php $this->endWidget(); ?>
</div><!-- form -->

* name and description is field on database
* method default is post and the data is send to current controller and current action, that's why after you click submit button the variable will be created and line 3 in controller will be executed

Post a Comment

Harap gunakan bahasa yang baik dan sopan, terima kasih