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

You are viewing revision #4 of this wiki article.
This version may not be up to date with the latest version.
You may want to view the differences to the latest version.

next (#5) »

  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.

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

    <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>
    
    <div class="row">
        <label>Designation</label>
        <input type="text" name="designation" value="<?php echo $model->designation; ?>"/>
        <span><?php if(isset($error['designation'])) echo $error['designation'][0]; ?></span>
    </div>
    
    <div class="row">
        <label>Manager</label>
        <input type="text" name="manager" value="<?php echo $model->manager; ?>"/>
        <span><?php if(isset($error['manager'])) echo $error['manager'][0]; ?></span>
    </div>
    
    <div class="row">
        <label>Profile Pic</label>
        <input type="file" name="profilepic"/>
        <span><?php if(isset($error['profilepic'])) echo $error['profilepic'][0]; ?></span>
    </div>

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

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);
		if(Yii::app()->request->isPostRequest)
		{
                        $model->profilepic = CUploadedFile::getInstanceByName('profilepic');
			$model->attributes=$_POST; 
                        
                        if($model->save())
                        {
                            print_r($model->profilepic);
                            $model->profilepic->saveAs(Yii::app()->basePath.'/../uploads/admin/profile/'.$model->profilepic); 
                            $this->redirect(array('create','msg'=>'Successfully Created User'));
                        }
		}
                
		$this->render('create',array(
			'model'=>$model,
		));
	}

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

2 2
6 followers
Viewed: 22 305 times
Version: Unknown (update)
Category: How-tos
Written by: kalpit
Last updated by: CeBe
Created on: Nov 28, 2013
Last updated: 10 years ago
Update Article

Revisions

View all history

Related Articles