How to set the values to textInput in Yii2

Hi, I am using yii2 basic application template. I am desiging a web portal for SHG. Here the modules are groupdetails where the user creates new group profile and member details.
The second module is Groupsaving details where each month group transaction details are recorded and maintained.

The process is when a new group is created with 10 members then they do total saving of that month. Here Opening balance of first month is set to 0, Total saving for example is 1000, Loan recovery is set to 0, Loan Interest is 0, fine is 0 Bank Interest is 0. Now Expense is 50, Loan Given is 0 and Bank Loan is 0.

So the formula is
Closing balance=(Opening Balance+TotalSaving+LoanRecovery+LoanInterest+Fine+BankInterest-Expense-LoanGiven-BankLoan.)

Total Value of Loan Given = Current month Loan given+all previous month loan given

Total Value of Loan Outstanding= Total Value of Loan Given - Loan Repaid uptil now

So I have groupdetails CRUD and groupsaving ddetails CRUD.

Now In Group saving details Controller actionCreate() I have to check whether there exists a record less than or equal to entered year, entered month and selected group. If record does not exists, the entry is first time.

$count = Yii::$app->db->createCommand('SELECT count(*) 
 FROM `groupsavingdetails`, groupdetails
 where groupdetails.GroupId=groupsavingdetails.GroupId
 and groupsavingdetails.Year<='.$model->Year.'
 and groupsavingdetails.Month<='.$model->Month.'
 and groupsavingdetails.GroupId='.$model->GroupId
	 )
						->queryScalar();



 if($count==0)
{
 $model->OpeningBalance=0;
 $model->TotalValueofLoanGiven=0;
 $model->LoanRepaidUptilNow= 0;
 $model->TotalValueOfLoanOutstanding = 0;

 $model->ClosingBalance=($model->OpeningBalance+$model->TotalSaving+$model->LoanRecovery+$model->LoanInterest+$model->Fine+$model->BankInterest-$model->Epense-$model->LoanGiven-$model->BankLoan);


 }

So Opening balance, Totalvalue of Loan Given, Loan repaid uptil now , Total value of loan outstanding and closing balance fields are disabled.

So how should I set the values as 0 to the fields using controller or jquery in form. How these values will get updated as soon as I change the values in other textInputs. This calculation is a cumulative calculation where first month closing balance will be next month opening balance and as such. How should I assign values as Opening Balance to 0 on the form field from controller or any different way.

Hi James,

First of all please don’t post your business logic here in the forums.

If you want to set default values of a model attribute follow the below example

[['OpeningBalance', 'TotalValueofLoanGiven'], 'default', 'value'=> 0]

Update your rule() function in your model you are done!

Ok, that’s fine. But how should I display the default value 0 assigned to Opening Balance field on the form which is set to readonly property true. And how should I change those values based on the selection of Groups, year and month?

I have a function which checks if the groupsavingdetails exists for selected group, year and month. If there is no record then 0 will be set, otherwise previous months values will be displayed

public function actionGroupsavinginfo($id,$year,$month)
    {
        $group = Yii::$app->db->createCommand('SELECT count(*) 
 FROM `groupsavingdetails`, groupdetails
 where groupdetails.GroupId=groupsavingdetails.GroupId
 and groupsavingdetails.Year<='.$year.'
 and groupsavingdetails.Month<='.$month.'
 and groupsavingdetails.GroupId='.$id
	 )
						->queryScalar(); 
 

 $groupsavingdetails = Yii::$app->db->createCommand('SELECT * 
 FROM `groupsavingdetails`
where
  groupsavingdetails.Year<='.$year.'
 and groupsavingdetails.Month<='.$month.'
 and groupsavingdetails.GroupId='.$id
	 )
						->queryAll(); 

        if ($group == 0) {

               echo $groupsavingdetails['OpeningBalance']=$groupsavingdetails['TotalValueofLoanGiven']=$groupsavingdetails['LoanRepaidUptilNow']=$groupsavingdetails['TotalValueOfLoanOutstanding']=0;
            }
         else {
            foreach($groupsavingdetails as $groupsavingdetails1)
			 {
			
			    echo $groupsaving_details['OpeningBalance'] = $groupsavingdetails1['ClosingBalance'];
				
				echo $groupsaving_details['TotalValueofLoanGiven']= $groupsavingdetails1['TotalValueofLoanGiven'];
				
				echo $groupsaving_details['LoanRepaidUptilNow']=$groupsavingdetails1['LoanRepaidUptilNow'];

				
				echo $groupsaving_details['TotalValueOfLoanOutstanding']= $groupsavingdetails1['TotalValueOfLoanOutstanding'];
		
			}
        }
    }

In the form I have called this method and set the values as follows:

<?= $form->field($model, 'Month',['enableAjaxValidation' => true])->dropDownList(['01'=>'January', '02' => 'February','03'=>'March','04'=>'April','05'=>'May','06'=>'June','07'=>'July','08'=>'August','09'=>'September','10'=>'October','11'=>'November','12'=>'December'], ['prompt'=>'Select Month','onChange'=>'
            

	$.post("index.php?r=groupsavingdetails/groupsavinginfo&id='.'"+$("select#groupsavingdetails-groupid").val()+"&year='.'"+$("input#groupsavingdetails-year").val()+"&month='.'"+$("select#groupsavingdetails-month").val(),function(data){
						
			$("input#groupsavingdetails-openingbalance").val(data);
						$("input#groupsavingdetails-totalvalueofloangiven").val(data);
						$("input#groupsavingdetails-loanrepaiduptilnow").val(data);
						$("input#groupsavingdetails-totalvalueofloanoutstanding").val(data);
	  
	  });	
	']) ?>

So next time, when the record exists for query, then the values are getting fetched correctly but same values getting displayed on all four fields. The function returns 4 values but all the 4 values are displayed in every inputs.