我正在尝试为Yii2创建FormGenerator组件。但我无法弄清楚如何设计形式的外观。所以我有一个FormGenerator类,应该动态创建表单视图文件。首先,我创建这个类的实例并给它们Model并调用$ formgenerate-> generate()方法。比在generate()方法中通过TableSchema收集模型的所有字段并调用renderForm()方法,其中创建表单的外观并将它们保存到视图文件中并返回此视图文件的路径这是我的代码:
public function renderForm()
{
$r = $l = "";
$lf = array_merge($this->fields["inputtext"], $this->fields["text"], $this->fields["others"]);
$rf = array_merge($this->fields["date"], $this->fields["dropdown"], $this->fields["fileimage"], $this->fields["filedocs"]);
$left = count($lf);
$right = count($rf);
$path = \Yii::$app->controller->getViewPath() . "/" . $this->getSrc() . ".php";
if(!is_dir( \Yii::$app->controller->getViewPath()."/form" )){
$mask = umask(0);
mkdir(\Yii::$app->controller->getViewPath() . "/form", 0777,true);
umask($mask);
}
$enc = "";
$content = "<?php\r\n/**\r\n* . \r\n* Author: Rashidov Nuriddin\r\n* Date: " . date("d.m.Y") . "\r\n* Time: " . date("H:i") . "\r\n*/\r\n?>";
if ($this->modelClass->upload_file) $enc = "'enctype' => 'multipart/form-data',";
$content .= "<?php \$form = \\yii\\bootstrap\\ActiveForm::begin(['id' => 'form_id','options' => ['class' => 'form-" . $this->layouttype . "'," . $enc . "],'fieldConfig' => [
'template' => '<div class=\"row\">{label}<div class=\"col-sm-10\">{input}</div><div class=\"col-sm-10\">{error}</div></div>',
'labelOptions' => ['class' => 'col-sm-2 control-label'],
],]);?>\r\n";
if ($this->layouttype == "horizontal") {
if ($left > $right) {
$i = 1;
$add = ceil(($left - $right) / 2);
foreach ($lf as $key => $val) {
if ($i == count($rf) + $add) break;
$l .= "<?=" . $val . "?>\r\n";
$i++;
unset($lf[$key]);
}
foreach ($lf as $key => $val) {
$r .= "<?=" . $val . "?>\r\n";
unset($lf[$key]);
}
foreach ($rf as $key => $val) {
$r .= "<?=" . $val . "?>\r\n";
unset($rf[$key]);
}
} elseif ($right === $left) {
foreach ($lf as $key => $val) {
$l .= "<?=" . $val . "?>\r\n";
}
foreach ($rf as $key => $val) {
$r .= "<?=" . $val . "?>\r\n";
unset($rf[$key]);
}
} elseif ($right > $left) {
$i = 1;
$add = ceil(($right - $left) / 2);
foreach ($rf as $key => $val) {
if ($i == count($rf) + $add) break;
$r .= "<?=" . $val . "?>\r\n";
$i++;
unset($rf[$key]);
}
foreach ($rf as $key => $val) {
$r .= "<?=" . $val . "?>\r\n";
unset($rf[$key]);
}
foreach ($lf as $key => $val) {
$l .= "<?=" . $val . "?>\r\n";
unset($lf[$key]);
}
}
$content .= "<div class='col-md-6' style='padding: 25px 65px 25px 25px;border-right:2px solid #ccc;'>" . $l . "</div>\r\n<div class='col-md-6' style='padding:25px;'>" . $r . "</div>";
} elseif ($this->layouttype == "vertical") {
$fr = array_merge($lf, $rf);
$ver = "";
foreach ($fr as $f => $v) {
$ver .= "<?=" . $v . "?>\r\n";
}
$content .= "<div class='col-sm-12'>" . $ver . "</div>";
};
$content .= "<div class='col-md-12'><input class=\"btn btn-blue col-md-6 col-md-offset-3\" type=\"submit\" name=\"" . strtolower((new \ReflectionClass($this->modelClass))->getShortName()) . "\" value=\"Добавить\"></div>";
$content .= "<?\\yii\\bootstrap\\ActiveForm::end() ?>";
file_put_contents($path, $content);
return $this->getSrc();
}
此方法获取所有模型字段并按类型将它们设置为div容器。例如:文本,密码输入和textareas到左边的日期,fileinput到右边。我的问题是田地的位置不正确 请提出有关创建动态表单字段及其位置的任何想法,建议。感谢