• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

GhazaliTajuddin.com

Another Kuantan Blogger

  • Home
  • Blog
    • Kuantan
    • Foods
    • Technology
    • Health
  • Stock Photography
    • Senarai Microstock Agency
    • Membuka akaun contributor Shutterstock.com
    • Tips untuk 10 keping gambar pertama Shutterstock.com
    • Mengapa Shutterstock.com reject gambar / submission
    • Model Release
    • Bagaimana withdraw earning daripada Fotolia
    • Bagaimana untuk mengisi keyword kepada imej dengan menggunakan Adobe Photoshop
You are here: Home / General / Understanding Yii CFORM

Understanding Yii CFORM

March 24, 2012 by ghazalitajuddin 5 Comments

Yii Framework
Yii Framework

In this tutorial we discuss about the basic principal of MVC using Yii Framework. How to create Model extends from CFormModel which we collect data from it, to create related Controller and finishing at View.

1. Generate webapp from Command Prompt

yiic webapp myapps

2. Uncomment gii on myapps/protected/config/main.php. Set your prefered password. This allow us to use scaffolding modules provided by Yii Framework. Please remove this when you ready to deploy your application for security reason.

3. Access your gii module at your browser

http://localhost/myapps/index.php?r=gii.

4. Using gii module online forms, create a Controller, name it MyData, it should generate MyDataController.php. You can access it at root/myapps/controllers/MyDataController.php

5. Duplicate myapps/models/ContactForm.php, rename it as MyDataForm.php. This will act as your model. This model extends CFormModel class as we dont use Active Records.

6. Clean up MyDataForm.php like this

[php]
/**
* ContactForm class.
* ContactForm is the data structure for keeping
* contact form data. It is used by the ‘contact’ action of ‘SiteController’.
*/
class MyDataForm extends CFormModel
{
public $name;
public $email;
public $subject;

/**
* Declares the validation rules.
*/
public function rules()
{
return array(
// name, email, subject and body are required
array(‘name, email, subject’,’required’),
);
}

}[/php]

7. Open MyDataController.php, we will add two important action, actionShow() and actionEdit() method. The show method is to send data to View while edit method is to receive, validate, process and send to show method.

[php] class MyDataController extends Controller { public function actionIndex() { $this->render(‘index’);
}

public function actionEdit()
{
//create $data instance from MyDataForm class
$data = new MyDataForm;

//check if $_POST[‘MyDataForm’] carry data
if(isset($_POST[‘MyDataForm’])){

//assign $_POST[‘MyDataForm’] value to $data object
$data->attributes=$_POST[‘MyDataForm’];

//check if $data is valid or not empty
if($data->validate())

//if data is valid, send data to actionShow to view
$this->render(‘show’,array(‘data’=>$data));

//else if false, render edit page again
else
$this->render(‘edit’,array(‘data’=>$data));
}else
$this->render(‘edit’,array(‘data’=>$data));
}

public function actionShow()
{
//create $data instance from MyDataForm class
$data = new MyDataForm;

//check is attribute is set
if(isset($data->name))

//render view show
$this->render(‘show’,array(‘data’=>data));
else
//else if data not set, assign default value
$data->name="Nama";
$data->email="Email";
$data->subject="Subjek";

//render view show
$this->render(‘show’,array(‘data’=>$data));

}

}
[/php]

8. Duplicate root/myapps/protected/views/mydata/index.php. Rename it as show.php.

[php]

<?php
$this->breadcrumbs=array(
‘Data’=>array(‘/mydata’),
‘Show’,
);?>
<h1><?php echo $this->id . ‘/’ . $this->action->id; ?></h1>

<p>

<?php
echo $data->name;
echo $data->subject;
echo $data->email;
?>
</p>

[/php]

9. Duplicate again root/myapps/protected/views/mydata/index.php. Rename it as edit.php.

[php]

<?php
$this->breadcrumbs=array(
‘Data’=>array(‘/data’),
‘Show’,
);?>
<h1><?php echo $this->id . ‘/’ . $this->action->id; ?></h1>

<p>

<?php $this->pageTitle=Yii::app()->name . ‘ – Edit Message’; ?>
<p><?php echo CHtml::link(‘Back’, ‘/mydata/index’)?> </p>

<h1>Edit Our Message</h1>

<p>
<div class="yiiForm">
<?php echo CHtml::beginForm(); ?>
<?php echo CHtml::errorSummary($data); ?>
<div class="simple">
<?php echo CHtml::activeLabel($data,’name’); ?>
<?php echo CHtml::activeTextField($data,’name’); ?>
</div>

<div class="simple">
<?php echo CHtml::activeLabel($data,’email’); ?>
<?php echo CHtml::activeTextField($data,’email’); ?>
</div>

<div class="simple">
<?php echo CHtml::activeLabel($data,’subject’); ?>
<?php echo CHtml::activeTextField($data,’subject’); ?>
</div>

<div class="action">
<?php echo CHtml::submitButton(‘Submit’); ?>
</div>
<?php echo CHtml::endForm(); ?>
</div><!– yiiForm –>
</p>
</p>

[/php]

10. Lastly we customize the mainpage view for MyData which connect both edit view and show view. Open up  root/myapps/protected/views/mydata/index.php and customize like below

[php]

<?php
$this->breadcrumbs=array(
‘Data’,
);?>
<h1><?php echo $this->id . ‘/’ . $this->action->id; ?></h1>

<p>
Click here to view data <?php echo CHtml::link(‘here’,array(‘/Data/show’))?>
Click here to view data <?php echo CHtml::link(‘here’,array(‘/Data/edit’))?>
<p>

[/php]

Filed Under: General, Technology Tagged With: CakePHP, Controller, Framework, model, MVC, OOP, PHP, View, wordpress, yii, Yii Framework

Reader Interactions

Comments

  1. Christine says

    March 27, 2012 at 12:18 pm

    Lovely write, trendy webpage design, carry on the great work

    Reply
    • Ahmad Ghazali Ahmad Tajuddin says

      March 27, 2012 at 3:48 pm

      Thank you.

      Reply
  2. salang says

    April 9, 2012 at 2:43 pm

    Thank you for this great tutorial which is very useful for a beginner like me. On customizing the index.php, I stumbled upon
    the error as follows:

    Fatal error: Using $this when not in object context in C:\wamp\www\myapps\index.php on line 2

    Line 2 refers to .. $this->breadcrumbs=array ….

    Hope you can enlighten me on this.

    Reply
    • Ahmad Ghazali Ahmad Tajuddin says

      April 9, 2012 at 4:36 pm

      Its quite difficult for me to guess the error base on what you have paste here.

      But usually Fatal error: Using $this when not in object context in C:\wamp\www\myapps\index.php on line 2 means, the object $this is not initiated or created properly, before being use. Just looks back at any declarations that u just add or change.

      Thank you for reading. I just make this post for my personal reference. Thanks.

      Reply
  3. sugi says

    August 22, 2013 at 3:13 pm

    how create validator in class MyDataForm extends CFormModel? i want to when i create new user with that MyDataForm and email not duplicate or unique. How do it? thnks,

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

“Solat. Sabar. Syukur. Senyum. Sedekah.”

For Collaboration, Events & Review, kindly contact me at +6016[-]9212092 or click Whatsapp button on this page.

Sponsor

Recent Posts

BadMethodCallException Method Illuminate\Database\Eloquent\Collection::roles does not exist.

User Roles And Permissions Without Package Laravel 10

Laravel Many To Many Relationship

Makan malam bersama keluarga di Awangan Palace

Sarapan pagi di Warung Gulai Kawah

Recent Comments

  • helmi on Personal Tips Berhenti Merokok
  • ghazalitajuddin on Personal Tips Berhenti Merokok
  • helmi on Personal Tips Berhenti Merokok
  • ghazalitajuddin on Nasi Lemak Kukus Restoran Zaman. Otai masih berbisa.
  • ghazalitajuddin on Air tangki radiator Proton Exora cepat kering? Cuba tukar penutup radiator!
  • Mal on Nasi Lemak Kukus Restoran Zaman. Otai masih berbisa.
  • Firdaus on Air tangki radiator Proton Exora cepat kering? Cuba tukar penutup radiator!

My Link

  • Takaful Insurance Web

JJCM

Aneka Kuih Tradisional

Lunch di Nasi Kukus Alom

Makan malam bersama keluarga di Awangan Palace

Nasi Lemak Kukus Restoran Zaman. Otai masih berbisa.

Demong Cafe Kampung Raja Besut

Tags

bebas rokok berhenti merokok breakfast Controller Framework Gezzeg Photography & Design health jalan-jalan cari makan jalan-jalan cari makan kuantan jjcm jjcm kuantan Jurufoto Kuantan Kuantan Kuantan Photographer kuantan programmer kuantan web developer kuantan webmaster laravel merokok merbahayakan kesihatan model MVC nikmat rokok OOP Pahang Pahangtourism pahang tourism Photo Manipulation PHP rajalanun retired smoking revisit pahang 2018 shutterstock stop smoking stop smoking tips stop smoking withdrawal symptom tips tips berhenti merokok View visit malaysia 2020 visit pahang visitpahang white wordpress yii Yii Framework

Recent Posts

  • BadMethodCallException Method Illuminate\Database\Eloquent\Collection::roles does not exist.
  • User Roles And Permissions Without Package Laravel 10
  • Laravel Many To Many Relationship
  • Makan malam bersama keluarga di Awangan Palace
  • Sarapan pagi di Warung Gulai Kawah

Copyright © 2025 — Ghazali Tajuddin • All rights reserved. •