HOW TO CREATE TABULAR FORM AND ITS VALIDATION IN YII

Handling tabular data submission is not so hard as it seems to be. To create tabular data you only need to know the logic and maximum work is done by yii itself.
To handle tabular form submission all you need is to create a tabular form and add new row via jQuery from the front end.
Let’s create a scenario, we have the model declaration as follows:-
We have two tables
1. Transfer
  I. id
  II. financial_year
  III. transfer_challan
  IV. transfer_date
  V. from_location
  VI. to_location
  VII. is_cancelled
  VIII. create_time
  IX. create_user_id
  X. update_time
  XI. update_user_id
2.TransferIpl
  i. id
  ii. transfer_item
  iii. transfer_publication
  iv. transfer_quantity
  v. transfer_id
  vi. in_hand
  vii. stock_id

For each Transfer we have multiple TransferIpl.
Initially we render one instance of Transferipl model and clone it and make necessary changes on each ADD NEW button click.

Below is the code for _form.php


field($model->test1(), '[0]transfer_item', ['template' => "{input}\n{hint}\n{error}"])->dropDownList($model->ItemList(),['prompt'=>'Choose Receiving Item',]);?>
 field($model->test1(), '[0]transfer_publication', ['template' => "{input}\n{hint}\n{error}"])->dropDownList($model->PublicationList(),['prompt'=>'Select a Publication-',]);?>	  
field($model->test1(), '[0]transfer_quantity',['enableClientValidation' => false], ['template' => "{input}\n{hint}\n{error}"])->textInput()->label(false) ?>                       

Jquery to be executed for each Add New Button is as follows:-


var bid=0;
$("#add").click(function() {
++bid;
$man = $("table tbody tr:first").clone(true);


$man.find("input").each(function(i,j) {
$actionname = $(j).attr("id").split("-");
$(j).attr("name","TransferIpl"+"["+bid+"]"+"["+$actionname[2]+"]");
$(j).attr("id","transferipl"+"-"+bid+"-"+$actionname[2]);
$(this).parent().removeClass();
$(this).val("");
$(this).parent().addClass("form-group field-transferipl"+"-"+bid+"-"+$actionname[2]+" "+"required");

});


$man.find("select").each(function(i,j) {
$actionname = $(j).attr("id").split("-");
$(j).attr("name","TransferIpl"+"["+bid+"]"+"["+$actionname[2]+"]");
$(j).attr("id","transferipl"+"-"+bid+"-"+$actionname[2]);
$(this).parent().removeClass();
$(this).parent().addClass("form-group field-transferipl"+"-"+bid+"-"+$actionname[2]+" "+"required");

});
$("table tbody").append( $man );

});



var numberit = function() {
$(".tabular > table > tbody > tr").find("input").each(function(i,j) {
$actionname = $(j).attr("id").split("-");
$(j).attr("name","TransferIpl"+"["+bid+"]"+"["+$actionname[2]+"]");});
};



$("#delete").click(function() {
$("input:checkbox:checked").parents("tr").remove();
});



$("body").on("click","#remove",function(){$(this).parent().detach()});

And in the Transfer.php (Transfer model) create a function to return the new instance for Transferipl model.

public function test1()
    {
	$searchModel = new TransferIpl();
	
		return $searchModel;
	}

Paste the code below in Transfer Controller and update the logic as required.

    public function actionCreate()


{
    $model = new Transfer();
	$TransferIpl = new TransferIpl();
	if(isset($_POST['Transfer']))
    { 
	$model->attributes=$_POST['Transfer'];
	$model->is_cancelled = 0;
	if(isset($_POST['TransferIpl']))
      {
		$m=0;
	  if($model->from_location ===  $model->to_location){
		$m++;
	  }
		foreach($_POST['TransferIpl'] as $i=>$j)
        {	  
          $TransferIpls=new TransferIpl();
          $TransferIpls->attributes=$j;
		  $TransferIpls->locationvalidation = $model->from_location;
		  $isOk = $TransferIpls->validate();
		  if(!$isOk){
		  		  $m++;
		  }
		}
	   if($m===0)
      {
	   $model->save();
		foreach($_POST['TransferIpl'] as $i=>$j)
                {
                    $TransferIpls=new TransferIpl();
                    $TransferIpls->attributes=$j;
					
					
					$_fromstock = Stock::find()
								->where([
					'item' => $TransferIpls->transfer_item,
					'publication' => $TransferIpls->transfer_publication, 
					'location' => $model->from_location
								])
								->orderBy('id')
								->one();
								
					$_tostock = Stock::find()
								->where([
					'item' => $TransferIpls->transfer_item,
					'publication' => $TransferIpls->transfer_publication, 
					'location' => $model->to_location
								])
								->orderBy('id')
								->one();


				if($_fromstock){
				if (!$_tostock){
						$_tostock =new Stock();
					$_tostock->stock = $TransferIpls->transfer_quantity;				
					$_tostock->item = $TransferIpls->transfer_item; 
					$_tostock->publication = $TransferIpls->transfer_publication; 
					$_tostock->location = $model->to_location;
					}else{
					$_tostock->stock += $TransferIpls->transfer_quantity;
					}
				$_fromstock->stock -= $TransferIpls->transfer_quantity;
				$_fromstock->save(false);
				$_tostock->save(false);
					$TransferIpls->transfer_id = $model->id;
                    $TransferIpls->save(false);
				}
                }
			}
			}
		return $this->redirect(['view', 'id' => $model->id]);
	
	} else {
            return $this->render('create', [
                'model' => $model,
				'TransferIpl' => $TransferIpl,
            ]);
        }

}

About Eshwar Dubey

Leave a Reply

Your email address will not be published. Required fields are marked *