custom form yii php

custom form Yii PHP,In yii  when  a model is generated  it get all columns from database and it represent as some fields like text-box etc. So have ever though about how to add a user define fields well that super easy to do. so let’s start

Step 1.

First we need a model that in which we gonna add the field so i  have generated a model already which I’ll called as userdefine model (the model files will be located under protected/model/)  i am  gonna add only one field for now  you can add N.. number of fields

The first step   is to declare a public variable in the model under  class userdefine extends CActiveRecord {
so after you finish it will be like this.

class userdefine extends CActiveRecord {
public $yourvarible;
}
then you can add the $yourvariable   inside public function rules() { under  array('yourvariable','required'),
then you should add it under  public function attributeLabels() {
after adding it will like this public function attributeLabels()
{
return array('comment' => 'Your comment',
);
}

after finishing the STEP 1: the whole code look likes this.

class NgoDetails extends CActiveRecord
{
public $yourvarible;
public function rules()
{
array('yourvariable','required'),
);
}
public function attributeLabels()
{
return array('yourvariable' => 'Your Label name',
);
}
}

STEP 2:

In step2 we have add the userdefine variable in the view generated by Yii, i’ll be adding a text field.the view will be under (protected/views) go into your view folder that which you gonna need to add the userdefine field.

adding a text field is pretty much straight forward under your view folder goto  _form.php file this were yii renders the _form.php  to the create.php there should be already some field must generated it will be inside each <div =”row”></div> so under the ending of a div  we will add our new userdefine field with

ALSO READ  Using MySQL Indexes for Performance Optimization

this how it looks after finishing

<div class=”row”>
<?php echo $form->labelEx($model,’your label name); ?>
<?php echo $form->textField($model,’yourvariable‘,array(‘size’=>60,’maxlength’=>120)); ?>
<?php echo $form->error($model,’yourvariable‘); ?>
</div>

 

STEP 3.

So we are coming to the end so the last thing is to pass the value from the newly created text field to the  controller goto (protected/contoller) the goto your  controller that’s generated by Yii

inside your controller goto the action that you wanna pass the text field values so i am using my own action that i will tell you in  next post now i am using  myaction() in my action use we are passing the   id  to the function. the we should load the model to tell  Yii that from which model we are getting  the fields $model=$this->loadModel($id) to access the value that are passing from the text field we use

if(isset($_POST['NgoDetails'])) {  it will get all the value that are passing from a form to controller to. access the get values from the specific fields we use

$model->yourvariable;  after you finshed the code will look like this

public function actionRequest($id)
{
$model=$this->loadModel($id);
if(isset($_POST['NgoDetails']))

{
$mode->yourvariable;

}

So that’s it in simple three step well not simple but if you get used to it will be easily so see you again in next post. Cheers 😀 😀