Create/Develop View files with plain HTML (Without using ActiveForm Class)

  1. Form Explanation
  2. Controller Explanation

Today i want to share with you is "How can we write HTML Code in view files? I am writing this article because many times we can not use CActiveForm to design our form...

here i am going to show you simple User Cread(add) operation with plain HTML...

create new create.php file inside user directory.

$error= $model->getErrors();
<form method="post" action="<?php echo Yii::app()->createUrl(array('/user/create')); ?>" enctype="multipart/form-data">
    <div class="row">
        <input type="text" name="email" value="<?php echo $model->email; ?>"/>
        <span><?php if(isset($error['email'])) echo $error['email'][0]; ?></span>
    <div class="row">
        <input type="password" name="password"/>
        <span><?php if(isset($error['password'])) echo $error['password'][0]; ?></span>

    <div class="row">
        <label>Employee Id</label>
        <input type="text" name="employeeid" value="<?php echo $model->employeeid; ?>"/>
        <span><?php if(isset($error['employeeid'])) echo $error['employeeid'][0]; ?></span>
    <div class="row">
        <input type="text" name="designation" value="<?php echo $model->designation; ?>"/>
        <span><?php if(isset($error['designation'])) echo $error['designation'][0]; ?></span>
    <div class="row">
        <input type="text" name="manager" value="<?php echo $model->manager; ?>"/>
        <span><?php if(isset($error['manager'])) echo $error['manager'][0]; ?></span>
    <div class="row">
        <label>Profile Pic</label>
        <input type="file" name="profilepic"/>
        <span><?php if(isset($error['profilepic'])) echo $error['profilepic'][0]; ?></span>

    <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>

Form Explanation

i want to focus on the following two things here..

1. $error= $model->getErrors(); 
    this we are using to retrieve errors which is generated by our model class **rules** method. if it contains error then it will print error to their respective fields by this statement.
<?php if(isset($error['email'])) echo $error['email'][0]; ?>

pretty cool !

2. action="<?php echo Yii::app()->getBaseUrl(true).'/index.php/admin/user/create'?>"
     this we are using to send out form data to the our controllers so further we can deal with our form data. i think it is pretty straight forward.

Now i will show how our controller method should look like?

public function actionCreate()
		$model=new User;

		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
                        $model->profilepic = CUploadedFile::getInstanceByName('profilepic');
                            $this->redirect(array('create','msg'=>'Successfully Created User'));

Controller Explanation

let's focus on the following one thing :

  1. if(Yii::app()->request->isPostRequest) here you should use isPostRequest to check and retrieve the form data. and if it is set then retrieve it by $_POST.

pretty easy !

And you are done !!!!