$fieldRender->text($item), 'header' => $fieldRender->header($item), 'paragraph' => $fieldRender->paragraph($item), 'hidden' => $fieldRender->hidden($item), 'radio-group' => $fieldRender->radioGroup($item), 'checkbox-group' => $fieldRender->checkboxGroup($item), 'select' => $fieldRender->select($item), 'button' => $fieldRender->button($item), 'textarea' => $fieldRender->textArea($item), }; } $fields = implode("\n", $fields); $tpl = file_get_contents(Yii::getAlias('@common/modules/forms/widgets/templates/DynaView.tpl')); $tpl = preg_replace([ '/\{\$fields\}/' ], [ $fields ], $tpl); file_put_contents(Yii::getAlias('@common/modules/forms/runtime/' . $viewName . '.php'), $tpl); } public static function generateFormView2($id, $json) { $viewName = 'DynaView' . $id; $fields = []; foreach ($json as $item) { if (isset($item['name'])) { $item['name'] = str_replace( '-', '_', $item['name'] ); } if ($item['type'] == 'text') { $options = []; $options[] = isset($item['placeholder']) ? "'placeholder' => '{$item['placeholder']}'" : ""; $options[] = isset($item['value']) ? "'value' => '{$item['value']}'" : ""; $options[] = isset($item['maxlength']) ? "'maxlength' => true" : ""; $options = implode(',', array_filter($options)); $description = isset($item['description']) ? $item['description'] : ''; $fields[] = "field(\$model, '{$item['name']}')->textInput([{$options}])->hint('{$description}') ?>"; } elseif ($item['type'] == 'header' || $item['type'] == 'paragraph') { $fields[] = "<{$item['subtype']}>{$item['label']}"; } elseif ($item['type'] == 'textarea') { $options = []; $options[] = isset($item['rows']) ? "'rows' => {$item['rows']}" : ""; $options[] = isset($item['placeholder']) ? "'placeholder' => '{$item['placeholder']}'" : ""; $options[] = isset($item['value']) ? "'value' => '{$item['value']}'" : ""; $options = implode(',', array_filter($options)); $description = isset($item['description']) ? $item['description'] : ''; $fields[] = "field(\$model, '{$item['name']}')->textarea([{$options}])->hint('{$description}') ?>"; } elseif ($item['type'] == 'hidden') { $options = []; $options[] = isset($item['value']) ? "'value' => '{$item['value']}'" : ""; $options = implode(',', array_filter($options)); $fields[] = "field(\$model, '{$item['name']}')->hiddenInput([{$options}])->label(false) ?>"; } elseif ($item['type'] == 'radio-group') { $values = []; $selected = []; foreach ($item['values'] as $value) { $values[] = "'{$value['value']}' => '{$value['label']}'"; if (isset($value['selected']) && $value['selected'] == true) { $selected[] = "'{$value['value']}'"; } } $values = implode(',', $values); $selected = implode(',', $selected); if ($selected) { $fields[] = "{$item['name']} = [{$selected}] ?>"; } $options = []; //$options[] = $selected ? "'value' => [{$selected}]" : ""; $options = implode(',', array_filter($options)); $description = $item['description'] ?? ''; $fields[] = "field(\$model, '{$item['name']}') ->radioList([ {$values} ], [{$options}])->hint('{$description}'); ?>"; } elseif ($item['type'] == 'checkbox-group') { $values = []; $selected = []; foreach ($item['values'] as $value) { $values[] = "'{$value['value']}' => '{$value['label']}'"; if (isset($value['selected']) && $value['selected'] == true) { $selected[] = "'{$value['value']}'"; } } $values = implode(',', $values); $selected = implode(',', $selected); if ($selected) { $fields[] = "{$item['name']} = [{$selected}] ?>"; } $options = []; //$options[] = $selected ? "'value' => [{$selected}]" : ""; $options = implode(',', array_filter($options)); $description = $item['description'] ?? ''; $fields[] = "field(\$model, '{$item['name']}') ->checkboxList([ {$values} ], [{$options}])->hint('{$description}'); ?>"; } elseif ($item['type'] == 'select') { $values = []; $selected = []; foreach ($item['values'] as $value) { $values[] = "'{$value['value']}' => '{$value['label']}'"; if (isset($value['selected']) && $value['selected'] == true) { $selected[] = "'{$value['value']}'"; } } $values = implode(',', $values); $selected = implode(',', $selected); $options = []; $options[] = $selected ? "'value' => [{$selected}]" : ""; $options[] = isset($item['placeholder']) ? "'prompt' => '{$item['placeholder']}'" : ""; $options[] = isset($item['multiple']) && $item['multiple'] == true ? "'multiple' => 'multiple'" : ""; $options = implode(',', array_filter($options)); $description = $item['description'] ?? ''; $fields[] = "field(\$model, '{$item['name']}')->dropDownList([{$values}], [{$options}])->hint('{$description}') ?>"; } elseif ($item['type'] == 'button' && $item['subtype'] == 'submit') { $fields[] = <<