Dynamic generated form in Yii2
I am learning programming within yii2 and I cannot figure out how to achieve a specific task: I want to generate monthly statistics based on collected data. For this task, I need to create a form that will be wary of the number of projects and users. For example:
Project 1: 500$
user1 field (in fields I want to put percentage of above value)
user2 field
user3 field
project 2: 1000$
user2 field
user3 field
Etc. It's easy to do it structurally and save the results in a serialized form, but this way I can't verify (for example, if the sum of the values in the fields for a given project exceeds 100%, and if there was an error, the data will be erased after the form is submitted). Is it possible to achieve such a task with Yii2?
edit: I am currently generating a form like this:
controller:
$date = "2015-05";
$model = new Raport();
$invoices = Faktura::find()->where(['LIKE','paid_date', $date])->all();
$projects = array();
foreach($invoices as $invoice){
$id = $invoice->project_id;
$projects[$id]['faktury'][] = $invoice;
$projects[$id]['model']= Project::find()->where(['id'=>$invoice->project_id])->one();
$projects[$id]['value']+= $invoice->value_netto;
$projects[$id]['users']= '' ;
$checks = Check::find()->where(['project_id'=>$id])->all();
if (empty($checks)){
$projects[$id]['users']=$projects[$id]['model']->users;
}else {
foreach ($checks as $check) {
$projects[$id]['users'][$check->user->id] = $check->user;
}
}
}
View:
foreach ($projects as $key => $project) {
echo"<h2>".$project['model']->name."</h2>";
echo 'project value: '.$project['value'];
echo"<p>percentage value:</p>";
// echo"<pre>";
foreach ($project['users'] as $user) {
echo"<p>".$user->email."</p>";
echo "<input name='Raport[".$key."][".$user->id."]'>";
}
}
source to share
Yes, you may already be creating a form in a dynamic way, create model rules in the same dynamic way.
What you are trying to do is already done in the GII generator. Gii accepts some fields (in the fields of the GII case database table) and it created rules for the model and the field in the form. Eventually Gii writes these rules to the model file, but you don't need to, you can simply return that array as the result of the form. The same goes for fields in the form, you can always have
Basically, you should create a model that is not an Active Record model like this https://github.com/yiisoft/yii2-app-advanced/blob/master/frontend/models/ContactForm.php . Instead of creating a rules function, you should create this array based on "other things" (for example, your projects and users). You should create a function that does validation and that checks if the value is greater than% 100 and binds it to validation of some fields. A simple example would be
/**
* @inheritdoc
*/
public function rules()
{
$rules = [];
foreach(['p1', 'p2'] as $project) {
foreach(['u1', 'u2'] as $user) {
$rules[] = [$project.'_'.$user, "required"];
}
}
return $rules;
}
Then, based on the same "other things" as above, you should create your shape. Since the model and the view are using the same fields, everything should be fine.
View example
<?php $form = ActiveForm::begin(); ?>
<?php
foreach(['p1', 'p2'] as $project) {
foreach(['u1', 'u2'] as $user) {
echo $form->field($model, $project.'_'.$user)->textInput()
}
}
?>
...................
<?php ActiveForm::end(); ?>
source to share