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


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 !!!!

Total 2 comments

#15714 report it
marcovtwout at 2013/12/09 09:36am

I'm afraid you are totally missing the point of coding in a framework.

One major point of using a framework is to avoid writing repetetive code. You would only have to fall back to 'custom' code when the framework does not provide you with enough flexibility to do this.

You say you cannot always use CActiveForm, but you don't specify a use case where this would be needed. Why would you replace perfectly flexible CHtml::error($model, 'email') with ugly and error prone $error= $model->getErrors(); if(isset($error['email'])) echo $error['email'][0];? The same for using createUrl()..

#15644 report it
pmaselkowski at 2013/12/03 07:58am
Use createUrl instead of string concatenation

As action in your form you use hardcoded url with getBaseUrl prefix. However Yii provides simple method for creating urls, you should use this:


Check this topic

Leave a comment

Please to leave your comment.

Write new article
  • Written by: kalpit
  • Updated by: CeBe
  • Category: How-tos
  • Yii Version: 1.1
  • Votes: +2 / -3
  • Viewed: 13,374 times
  • Created on: Nov 28, 2013
  • Last updated: Apr 14, 2014
  • Tags: views, tutorial