text($item); break; case 'header': $fields[] = $fieldRender->header($item); break; case 'paragraph': $fields[] = $fieldRender->paragraph($item); break; case 'hidden': $fields[] = $fieldRender->hidden($item); break; case 'radio-group': $fields[] = $fieldRender->radioGroup($item); break; case 'checkbox-group': $fields[] = $fieldRender->checkboxGroup($item); break; case 'select': $fields[] = $fieldRender->select($item); break; case 'button': $fields[] = $fieldRender->button($item); break; case 'textarea': $fields[] = $fieldRender->textArea($item); break; } } $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 = isset($item['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 = isset($item['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 = isset($item['description']) ? $item['description'] : ''; $fields[] = "field(\$model, '{$item['name']}')->dropDownList([{$values}], [{$options}])->hint('{$description}') ?>"; } elseif ($item['type'] == 'button' && $item['subtype'] == 'submit') { $fields[] = <<