You are viewing revision #6 of this wiki article.
This is the latest version of this article.
You may want to see the changes made in this revision.
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()->createUrl(array('/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 :
- 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 !!!!
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:Yii::app()->createUrl(array('/admin/user/create'))
Check this topic
Critique
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()..If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.