Create Custom Form

Hi Guys!

First I wanna say hello to everyone, 'cause this is my first post here :)

I’ve to say, that I’m very new to YII and the whole MVC thing. So here’s my problem:

I’ve already created 3 models for 3 different DB tables. No I tried to generate a custom form, with who I want to get some data for a custom query. I wanted to try the GII-Form-Builder, but it seems that it only works for db-driven models.

So my question is: how can I create a custom form from scratch? I already raped Google with my question and also tried to work through the CForm Doc etc… maybe someone can help me out with a small snippet :)

cheers and thanks to everyone in advance!


You can look at example models, ContactForm and LoginForm.

They’re created by ‘yiic webapp’ command.

Here’s the ContactForm;



 * ContactForm class.

 * ContactForm is the data structure for keeping

 * contact form data. It is used by the 'contact' action of 'SiteController'.


class ContactForm extends CFormModel


    public $name;

    public $email;

    public $subject;

    public $body;

    public $verifyCode;


     * Declares the validation rules.


    public function rules()


        return array(

            // name, email, subject and body are required

            array('name, email, subject, body', 'required'),

            // email has to be a valid email address

            array('email', 'email'),

            // verifyCode needs to be entered correctly

            array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),




     * Declares customized attribute labels.

     * If not declared here, an attribute would have a label that is

     * the same as its name with the first letter in upper case.


    public function attributeLabels()


        return array(

            'verifyCode'=>'Verification Code',




and here is the corresponding view:

<?php $form=$this->beginWidget('CActiveForm', array(






)); ?>

    <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,'name'); ?>

        <?php echo $form->textField($model,'name'); ?>

        <?php echo $form->error($model,'name'); ?>


    <div class="row">

        <?php echo $form->labelEx($model,'email'); ?>

        <?php echo $form->textField($model,'email'); ?>

        <?php echo $form->error($model,'email'); ?>


    <div class="row">

        <?php echo $form->labelEx($model,'subject'); ?>

        <?php echo $form->textField($model,'subject',array('size'=>60,'maxlength'=>128)); ?>

        <?php echo $form->error($model,'subject'); ?>


    <div class="row">

        <?php echo $form->labelEx($model,'body'); ?>

        <?php echo $form->textArea($model,'body',array('rows'=>6, 'cols'=>50)); ?>

        <?php echo $form->error($model,'body'); ?>


    <?php if(CCaptcha::checkRequirements()): ?>

    <div class="row">

        <?php echo $form->labelEx($model,'verifyCode'); ?>


        <?php $this->widget('CCaptcha'); ?>

        <?php echo $form->textField($model,'verifyCode'); ?>


        <div class="hint">Please enter the letters as they are shown in the image above.

        <br/>Letters are not case-sensitive.</div>

        <?php echo $form->error($model,'verifyCode'); ?>


    <?php endif; ?>

    <div class="row buttons">

        <?php echo CHtml::submitButton('Submit'); ?>


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

Thanks! :)

I copied the Contact form files and got model, view and controller files. (name: CSVGenerator)

But now I’m stuck again: How do I tell the form, which action to execute, after the form is being submitted?

Sorry for my quite stupid questions, but I don’t get the whole MVC thingy right now ;)



The CFormModel acts only as Validation Layer. It does not have any actions. If you want actions, you need a controller.

class ContactController extends CController {

   public function actionForm()


         //this will be your form...put your CFormModel here.



I recommend you install the debug toolbar extension so you can easily see what is being POSTed back to your server.

Are your 3 tables related - they share some sort of foreign key relationship and you’ve defined relations() in your models?

If so, in your controller you create an instance of each model and assign to view. You also create if{} blocks where you test if input is received from the form, like:

if(isset($_POST[‘modelA’])) then do something. Or, if related data:


This may help you: