通过PHP渲染form部分,生成表单,直接放到模版里边使用,直接上代码:
<?php class FormExcetion extends \Exception { } /** * title fieldType name validateRules [options value class subtitle tips, placeholder] * option 仅在radio checkbox select */ class Form { /** * form 的method属性 * * @var string $name */ private $name = 'ajaxForm'; /** * form 的method属性 * * @var mixed|string $method */ private $method = 'post'; /** * form的action属性 * * @var string $action */ private $action = ''; /** * form 的onsubmit属性 * * @var mixed|string */ private $onsubmit = ''; /** * form需要上传文件就需要设置成 multipart/form-data * * @var mixed|string */ private $encrypt; /** * @var $array $fileds */ private $fields = []; /** * 设置值,避免在添加表单 * @var array $values */ private $values = []; /** * 追加 html * @var array */ private $appendHtml = []; /** * 需要引入的css * * @var array $dependenceCSS */ private $dependenceCSS = []; /** * 添加依赖CSS Block * @var array */ private $dependenceCSSBlock = []; /** * 需要引入的js * * @var array $dependenceJS */ private $dependenceJS = []; /** * 添加依赖JS Block * @var array */ private $dependenceJSBlock = []; /** * form 表单的callback * * @var string $callback */ private $callback = ''; private $handleBySelf = 0; public function __construct($name, $method = 'post', $action, $onsubmit = '', $encrypt = 'application/x-www-form-urlencoded') { $this->name = $name; $this->method = $method ?? 'post'; $this->action = $action; $this->onsubmit = $onsubmit; $this->encrypt = $encrypt ?? 'application/x-www-form-urlencoded'; $this->clean(); } /** * clean array * @return void */ public function clean() { $this->fields = []; $this->values = []; } public function addHidden($title, $name, $options = []){ $this->fields[] = ['hidden', $title, $name, $options]; return $this; } /** * 批量添加输入框 * * @param $inputs * @return $this * @throws FormExcetion */ public function addInputs($inputs = array()) { if(!is_array($inputs)) { throw new FormExcetion("元素不能为空"); } foreach ($inputs as $input) { list($title, $name, $options) = $input; $this->addInput($title, $name, $options); } return $this; } /** * 标签输入 * @param array $inputs * @return void * @throws FormExcetion */ public function addTags($inputs = array()) { if(!is_array($inputs)) { throw new FormExcetion("元素不能为空"); } foreach ($inputs as $input) { list($title, $name, $options) = $input; $this->addTag($title, $name, $options); } } /** * 标签输入框 * * @param string $title 名称 * @param string $name input名称 * @param array $options 其他选项[value option] * @return void */ public function addTag($title, $name, $options) { $this->addDependenceCSS(_link('static/assets/plugins/bootstrap-tagsinput/dist/bootstrap-tagsinput.css')); $this->addDependenceJS(_link('static/assets/plugins/bootstrap-tagsinput/dist/bootstrap-tagsinput.min.js')); $this->fields[] = ['tag', $title, $name, $options]; } /** * @param $title * @param $name * @param $options * @return $this */ public function addInput($title, $name, $options = []) { $this->fields[] = ['input', $title, $name, $options]; return $this; } /** * 密码输入框 * @param $title * @param $name * @param $options * @return $this */ public function addPassword($title, $name, $options = []) { $this->fields[] = ['password', $title, $name, $options]; return $this; } /** * add radio * @param $title * @param $name * @param $options * @return $this */ public function addRadio($title, $name, $options = []) { $this->fields[] = ['radio', $title, $name, $options]; return $this; } public function addCheckbox($title, $name, $options = []) { $this->fields[] = ['checkbox', $title, $name, $options]; return $this; } /** * 批量生成checkboxs * @param array $checkboxs * @return $this * @throws FormExcetion */ public function addCheckboxs($checkboxs) { if(!is_array($checkboxs)) { throw new FormExcetion("元素不能为空"); } foreach ($checkboxs as $checkbox) { list($title, $name, $options) = $checkbox; $this->addCheckbox($title, $name, $options); } return $this; } public function addSelect($title, $name, $options = []) { $options['id'] = $options['id'] ?? randomStr(5); $this->addDependenceCSS(_link('static/assets/plugins/bootstrap-select/bootstrap-select.min.css')); $this->addDependenceCSS(_link('static/assets/plugins/multiselect/css/multi-select.css')); $this->addDependenceJS(_link('static/assets/plugins/select2/dist/js/select2.full.min.js')); $this->addDependenceJS(_link('static/assets/plugins/bootstrap-select/bootstrap-select.min.js')); $this->addDependenceJS(_link('static/assets/plugins/multiselect/js/jquery.multi-select.js')); $this->addDependenceJSBlock(' $("#'.$options['id'].'").selectpicker(); '); $this->fields[] = ['select', $title, $name, $options]; return $this; } /** * 单选select * * @param array $selects 显示名称 * @return form */ public function addSelects($selects) { if(!is_array($selects)) { throw new FormExcetion("元素不能为空"); } foreach ($selects as $select) { list($title, $name, $options) = $selects; $id = randomStr(5); $options['id'] = $options['id'] ?? $id; $this->addSelect($title, $name, $options); } return $this; } /** * 多选 select * * @param string $tile 显示名称 * @param string $name select名称 * @param array $options [value option class subtitle tips, placeholder] * @return void */ public function addMultiSelect($tile, $name, $options = []) { $this->addDependenceCSS(_link('static/assets/plugins/bootstrap-select/bootstrap-select.min.css')); $this->addDependenceCSS(_link('static/assets/plugins/multiselect/css/multi-select.css')); $this->addDependenceJS(_link('static/assets/plugins/select2/dist/js/select2.full.min.js')); $this->addDependenceJS(_link('static/assets/plugins/bootstrap-select/bootstrap-select.min.js')); $this->addDependenceJS(_link('static/assets/plugins/multiselect/js/jquery.multi-select.js')); $options['id'] = $options['id'] ?? randomStr(5); $this->addDependenceJSBlock(' $("#'. $options['id'] .'").selectpicker({"noneSelectedText":"请选择"}); '); $this->fields[] = ['multiSelect', $tile, $name, $options]; return $this; } /** * 添加Textare * @param string $title textarea显示名称 * @param string $name textarea名称 * @param $options [value option class subtitle tips, placeholder] * @return void */ public function addTextarea($title, $name, $options = []) { $this->fields[] = ['textarea', $title, $name, $options]; return $this; } /** * 添加File到表单 * * @param string $title 名称 * @param string $name file input name * @param $options [value option class subtitle tips, placeholder] * @return $this * * messages: { * 'default': 'Drag and drop a file here or click', * 'replace': 'Drag and drop or click to replace', * 'remove': 'Remove', * 'error': 'Ooops, something wrong appended.' * }, * error: { * 'fileSize': 'The file size is too big ({{ value }} max).', * 'minWidth': 'The image width is too small ({{ value }}}px min).', * 'maxWidth': 'The image width is too big ({{ value }}}px max).', * 'minHeight': 'The image height is too small ({{ value }}}px min).', * 'maxHeight': 'The image height is too big ({{ value }}px max).', * 'imageFormat': 'The image format is not allowed ({{ value }} only).' }, */ public function addFile($title, $name, $options = []) { $this->addDependenceCSS(_link("static/assets/plugins/dropify/dist/css/dropify.min.css")); $this->addDependenceJS(_link("static/assets/plugins/dropify/dist/js/dropify.min.js")); $this->addDependenceJSBlock(' dropfyFile = $(\'.dropify\').dropify('. '{'. 'messages:{default:"拖拽文件到这或点击这里",remove:"删除",replace:"拖拽文件到这里或点击这里替换",error:"发生错误,请重试"},'. 'error:{fileSize:"文件太大(最大允许{{ value }})",minWidth:"图片太窄(最小{{ value }}px)",'. 'maxWidth:"文件宽度太大(最大{{ value }}px)",minHeight:"文件高度太小(最小{{ value }}px)",'. 'maxHeight:"文件高度太长(最大高度{{ value }}px)",'. 'imageFormat:"文件格式不正确(需要{{ value }})",'. 'fileExtension:"文件格式不正确(需要{{ value }})"'. '}'. '});'); $this->fields[] = ['file', $title, $name, $options]; return $this; } /** * 添加依赖的css * * @param string $css css全路径 * @return $this */ public function addDependenceCSS($css) { if(!$css) return $this; if(in_array($css, $this->dependenceCSS)) { return $this; } $this->dependenceCSS[] = $css; return $this; } /** * 添加依赖的css bock * * @param string $cssBlock * @return $this */ public function addDependenceCSSBlock($cssBlock) { if(!$cssBlock) return $this; if(in_array($cssBlock, $this->dependenceCSSBlock)) { return $this; } $this->dependenceCSSBlock[] = $cssBlock; return $this; } /** * 添加js依赖 * * @param string $js js文件 * @return $this */ public function addDependenceJS($js) { if(!$js) return $this; if(in_array($js, $this->dependenceJS)) { return $this; } $this->dependenceJS[] = $js; return $this; } /** * 添加依赖的js block * * @param $jsBlock * @return $this */ public function addDependenceJSBlock($jsBlock) { if(!$jsBlock) return $this; if(in_array($jsBlock, $this->dependenceJSBlock)) { return $this; } $this->dependenceJSBlock[] = $jsBlock; return $this; } /** * 渲染css * * @return string */ public function renderCSS() { $css = []; foreach ($this->dependenceCSS as $link) { $css[] = '<link href="'. $link .'" rel="stylesheet">'; } return join("\n", $css); } /** * render css block * @return string */ public function renderCSSBlock() { $this->dependenceCSSBlock = []; return '<style>'."\n". join("\n", $this->dependenceCSSBlock) ."\n". '</style>'; } /** * render js * @return string */ public function renderJS() { $js = []; foreach ($this->dependenceJS as $link) { $js[] = '<script src="'.$link.'"></script>'; } $this->dependenceJS = []; return join("\n", $js); } /** * render js block * @return string */ public function renderJSBlock() { $js = []; $js[] = '<script>'; $js[] = ' var dropfyFile; $(function(){ '.join("\n", $this->dependenceJSBlock).' });'; $js[] = '</script>'; $this->dependenceJSBlock = []; return join("\n", $js); } /** * 渲染tips * @param string $tips * @return string */ private function renderTips($tips) { if($tips == '') return ''; return '<span class="help-block"><small>'. $tips .'</small></span>'; } /** * 渲染hidden 只使用name和value * * @param string $title 左侧title * @param string $name input的name属性 * @param array $options [value option class subtitle tips, placeholder] * @return string */ private function renderHidden($title, $name, $options = []) { $value = $options['value'] ?? $options['default'] ?? ''; $verify = isset($options['verify']) ? 'verify="'. $options['verify'].'"' : ''; return '<input type="hidden" name="'.$name.'" id="'. $name .'" value="'. $value .'" '. $verify .' />'; } /** * 渲染输入框 * @param string $title 左侧title * @param $name input的name属性 * @param $options * @return string */ private function renderInput($title, $name, $options = []) { $verify = isset($options['verify']) ? 'verify="'. $options['verify'].'"' : ''; $input = []; $input[] = '<div class="form-group row">'; $inputId = randomStr(5); $input[] = ' <label for="'. $inputId .'" class="col-md-2 col-form-label">'; $input[] = $title; if(isset($options['subtitle'])) { $input[] = ' <p class="card-subtitle"><small>'; $input[] = $options['subtitle']; $input[] = ' </small></p>'; } $input[] = ' </label>'; $value = $options['value'] ?? $options['default'] ?? ''; $input[] = ' <div class="col-md-10">'; $input[] = ' <input class="'.($options['class'] ??'form-control'). '" name="'. $name .'" type="text" value="'. $value . '" id="'. $inputId .'" rules="'.($options['rules'] ?? '').'" placeholder="'. ($options['placeholder'] ?? '') .'" '. $verify .'>'; $input[] = $this->renderTips($options['tips'] ?? ''); $input[] = ' </div>'; $input[] = '</div>'; return join("\n", $input); } /** * 密码输入框 * * @param $title * @param $name * @param $options * @return string */ public function renderPassword($title, $name, $options = []) { $verify = isset($options['verify']) ? 'verify="'. $options['verify'].'"' : ''; $input = []; $input[] = '<div class="form-group row">'; $inputId = randomStr(5); $input[] = ' <label for="'. $inputId .'" class="col-md-2 col-form-label">'; $input[] = $title; if(isset($options['subtitle'])) { $input[] = ' <p class="card-subtitle"><small>'; $input[] = $options['subtitle']; $input[] = ' </small></p>'; } $input[] = ' </label>'; $value = $options['value'] ?? $options['default'] ?? ''; $input[] = ' <div class="col-md-10">'; $input[] = ' <input class="'.($options['class'] ??'form-control'). '" name="'. $name .'" type="password" value="'. $value . '" id="'. $inputId .'" rules="'.($options['rules'] ?? '').'" placeholder="'. ($options['placeholder'] ?? '') .'" '. $verify .'>'; $input[] = $this->renderTips($options['tips'] ?? ''); $input[] = ' </div>'; $input[] = '</div>'; return join("\n", $input); } /** * 渲染radio * * @param string $title 显示文字 * @param string $name radio name * @param array $options [value option class subtitle tips, placeholder] * @return string */ private function renderRadio($title, $name, $options = []) { $radio = []; $radio[] = '<div class="form-group row">'; $radio[] = ' <label class="col-md-2 col-form-label">'; $radio[] = $title; if(isset($options['subtitle'])) { $radio[] = ' <p class="card-subtitle"><small>'; $radio[] = $options['subtitle']; $radio[] = ' </small></p>'; } $radio[] = ' </label>'; $radio[] = ' <div class="col-md-10 col-form-label">'; $radioGroup = $options['option'] ?? []; $value = $options['value'] ?? $options['default'] ?? ''; foreach ($radioGroup as $group) { list($groupName, $groupValue) = $group; $checked = ''; if(strlen($value) > 0 && $groupValue == $value) { $checked = 'checked="checked"'; } $radioId = randomStr(5); $radio[] = ' <input type="radio" class="'.($options['class'] ??'custom-control-input'). '" '. $checked .' name="'. $name . '" id="'. $radioId .'" value="'. $groupValue .'">'; $radio[] = ' <label for="'. $radioId .'">'; $radio[] = $groupName; $radio[] = ' </label>'; } $radio[] = $this->renderTips($options['tips'] ?? ''); $radio[] = ' </div>'; $radio[] = '</div>'; return join("\n", $radio); } /** * 渲染radio * * @param string $title 显示文字 * @param string $name radio name * @param array $options [value class subtitle tips, placeholder] * @return string */ private function renderCheckbox($title, $name, $options = []) { $checkbox = []; $checkbox[] = '<div class="form-group row">'; $checkbox[] = ' <label class="col-md-2 col-form-label">'; $checkbox[] = $title; if(isset($options['subtitle'])) { $checkbox[] = ' <p class="card-subtitle"><small>'; $checkbox[] = $options['subtitle']; $checkbox[] = ' </small></p>'; } $checkbox[] = ' </label>'; $checkbox[] = ' <div class="col-md-10 col-form-label">'; $checkboxGroup = $options['option'] ?? []; $value = (array) $options['value']?? []; foreach ($checkboxGroup as $group) { list($groupName, $groupValue) = $group; $checked = in_array($groupValue, $value) ? 'checked="checked"' : ''; $checkboxId = randomStr(5); $checkbox[] = ' <input type="checkbox" class="'.($options['class'] ??'custom-control-input'). '" '. $checked .' name="'. $name . '" id="'. $checkboxId .'" value="'. $groupValue .'">'; $checkbox[] = ' <label for="'. $checkboxId.'">'; $checkbox[] = $groupName; $checkbox[] = ' </label>'; } $checkbox[] = $this->renderTips($options['tips'] ?? ''); $checkbox[] = ' </div>'; $checkbox[] = '</div>'; return join("\n", $checkbox); } /** * 渲染select * @param string $title 左侧标题 * @param string $name select的name属性 * @param array $options * @return string */ public function renderSelect($title, $name, $options = []) { $verify = isset($options['verify']) ? 'verify="'. $options['verify'].'"' : ''; $select = []; $select[] = '<div class="form-group row">'; $select[] = ' <label class="col-md-2 col-form-label">'; $select[] = $title; if(isset($options['subtitle'])) { $select[] = ' <p class="card-subtitle"><small>'; $select[] = $options['subtitle']; $select[] = ' </small></p>'; } $select[] = ' </label>'; $select[] = ' <div class="col-md-10 col-form-label">'; $select[] = ' <select class="selectpicker" id="'. ($options['id'] ?? randomStr(5)).'" name="'. $name .'" rules="'. ($options['rules'] ?? '') .'" class="'.($options['class'] ??'').'" '. $verify .'>'; $selectGroup = $options['option'] ?? []; $value = $options['value'] ?? $options['default'] ?? ''; foreach ($selectGroup as $group) { list($groupName, $groupValue) = $group; $selected = ""; if($value == $groupValue) { $selected = 'selected="selected"'; } $select[] = '<option value="'. $groupValue .'" '. $selected .'>'. $groupName .'</option>'; } $select[] = ' </select>'; $select[] = $this->renderTips($options['tips'] ?? ''); $select[] = ' </div>'; $select[] = '</div>'; return join("\n", $select); } /** * 渲染 multi select * @param string $title 标题 * @param string $name 名称 * @param $options * @return string */ private function renderMultiSelect($title, $name, $options = []) { $verify = isset($options['verify']) ? 'verify="'. $options['verify'].'"' : ''; $multiSelect = []; $multiSelect[] = '<div class="form-group row">'; $multiSelect[] = ' <label class="col-md-2 col-form-label">'; $multiSelect[] = $title; if(isset($options['subtitle'])) { $multiSelect[] = ' <p class="card-subtitle"><small>'; $multiSelect[] = $options['subtitle']; $multiSelect[] = ' </small></p>'; } $multiSelect[] = ' </label>'; $multiSelect[] = ' <div class="col-md-10 col-form-label">'; $multiSelect[] = ' <select class="selectpicker" id="'. ($options['id'] ?? randomStr(5)).'" multiple="multiple" data-placeholder="请选择" name="'. $name .'" rules="'. ($options['rules'] ?? '') .'" '. $verify .' class="'.($options['class'] ??'').'">'; $multiSelectGroup = $options['option'] ?? []; $value = $options['value'] ?? $options['default'] ?? ''; foreach ($multiSelectGroup as $groups) { $multiSelect[] = '<optgroup label="'. $groups['group'].'">'; foreach($groups['data'] as $group) { list($groupName, $groupValue) = $group; $multiSelected = ""; if($value == $groupValue) { $multiSelected = 'selected="selected"'; } $multiSelect[] = '<option value="'. $groupValue .'" '. $multiSelected .'>'. $groupName .'</option>'; } $multiSelect[] = '</optgroup>'; } $multiSelect[] = ' </select>'; $multiSelect[] = $this->renderTips($options['tips'] ?? ''); $multiSelect[] = ' </div>'; $multiSelect[] = '</div>'; return join("\n", $multiSelect); } /** * 渲染File * @param string $title * @param string $name * @param string $options * @return string */ public function renderFile($title, $name, $options) { $extension = $options['extensions'] ?? ''; $allowedFileExtension = ''; if($extension) { $allowedFileExtension = ' data-allowed-file-extensions="'.$extension.'"'; } $id = randomStr(5); $file = []; $file[] = '<div class="form-group row">'; $file[] = ' <label class="col-md-2 col-form-label">'; $file[] = $title; $file[] = ' </label>'; $file[] = ' <div class="col-md-10 col-form-label">'; $file[] = '<input type="file" name="'. $name .'" id="input-file-'. $id .'" class="dropify" rules="'. ($options['rules'] ?? '') .'" data-default-file="'.($options['value']).'" data-max-file-size="'.($options['size'] ?? '2M').'" '. $allowedFileExtension .' />'; $file[] = $this->renderTips($options['tips'] ?? ''); $file[] = ' </div>'; $file[] = '</div>'; return join("\n", $file); } /** * 渲染textarea * @param string $title 显示名称 * @param string $name textare名称 * @param $options * @return string */ public function renderTextarea($title, $name, $options) { $verify = isset($options['verify']) ? 'verify="'. $options['verify'].'"' : ''; $id = randomStr(5); $textarea = []; $textarea[] = '<div class="form-group row">'; $textarea[] = ' <label class="col-md-2 col-form-label">'; $textarea[] = $title; $textarea[] = ' </label>'; $textarea[] = ' <div class="col-md-10 col-form-label">'; $textarea[] = '<textarea id="'. $id .'" name="'. $name .'" class="form-control" rows="'. ($options['rows'] ?? 5) .'" cols="'.( $options['cols'] ?? 100).'" rules="'.($options['rules'] ?? '').'" '. $verify .'>'. ($options['value'] ?? '') .'</textarea>'; $textarea[] = $this->renderTips($options['tips'] ?? ''); $textarea[] = ' </div>'; $textarea[] = '</div>'; return join("\n", $textarea); } /** * 渲染输入框 * @param string $title 左侧title * @param $name input的name属性 * @param $options * @return string */ private function renderTag($title, $name, $options = []) { $verify = isset($options['verify']) ? 'verify="'. $options['verify'].'"' : ''; $input = []; $input[] = '<div class="form-group row">'; $inputId = randomStr(5); $input[] = ' <label for="'. $inputId .'" class="col-md-2 col-form-label">'; $input[] = $title; if(isset($options['subtitle'])) { $input[] = ' <p class="card-subtitle"><small>'; $input[] = $options['subtitle']; $input[] = ' </small></p>'; } $input[] = ' </label>'; $value = $options['value'] ?? $options['default'] ?? ''; $input[] = ' <div class="col-md-10">'; $input[] = ' <input class="'.($options['class'] ??'form-control'). '" name="'. $name .'" type="text" value="'. $value . '" id="'. $inputId .'" data-role="tagsinput" rules="'.($options['rules'] ?? '').'" '. $verify .' placeholder="'. ($options['placeholder'] ?? '') .'" size="'.($options['size'] ?? 100).'">'; $input[] = $this->renderTips($options['tips'] ?? ''); $input[] = ' </div>'; $input[] = '</div>'; return join("\n", $input); } /** * 追加html用于复杂的form表单 * * @param string $html html内容 * @return void */ public function appendHtml($html) { $this->fields[] = ['html', $html, '', '', '']; return $this; } /** * @param array $formValue * @return void */ public function setValues($formValue) { if(!is_array($formValue)) { return; } $this->values = $formValue; } /** * 设置 callback * @param string $callback * @return $this|void */ public function setCallback($callback = '') { if($callback == '') { return $this; } $this->callback = $callback; } /** * 是否自己处理返回数据 * * @param int $isHandle 0/1 * @return void */ public function setHandleBySelf($isHandle = 0) { $this->handleBySelf = $isHandle; } public function insertAt($index = -1, $html) { $this->insertAt[$index] = $html; } /** * 返回渲染内容 * * @return string */ public function render() { $template = []; $callback = ''; if($this->callback != '') { $callback = 'data-callback="'. $this->callback .'"'; } $handleBySelf = ''; if($this->handleBySelf == 1) { $handleBySelf = 'data-handlebyself="'. $this->handleBySelf .'"'; } $template[] = '<form name="'.$this->name.'" method="'. $this->method .'" action="'. $this->action .'" '. $callback .' '. $handleBySelf .' enctype="'. $this->encrypt .'" onsubmit="'. $this->onsubmit .'">'; foreach ($this->fields as $field) { list($elementType, $title, $name , $options) = $field; if(isset($this->values[$name]) && !$options['value']) { $options['value'] = $this->values[$name]; } switch ($elementType) { case 'input': $template[] = $this->renderInput($title, $name , $options); break; case 'password': $template[] = $this->renderPassword($title, $name , $options); break; case 'radio': $template[] = $this->renderRadio($title, $name , $options); break; case 'checkbox': $template[] = $this->renderCheckbox($title, $name, $options); break; case 'hidden': $template[] = $this->renderHidden($title, $name, $options); break; case 'select': $template[] = $this->renderSelect($title, $name, $options); break; case 'multiSelect': $template[] = $this->renderMultiSelect($title, $name, $options); break; case 'textarea': $template[] = $this->renderTextarea($title, $name, $options); break; case 'file': $template[] = $this->renderFile($title, $name, $options); break; case 'tag': $template[] = $this->renderTag($title, $name, $options); break; case 'html': $template[] = $title; break; } } foreach ($this->appendHtml as $append) { $template[] = $append; } $template[] = "</form>"; $this->clean(); return join("\n", $template); } }
使用方法,php自动生成input等内容,在模版里渲染:
<?php $form = new Form('ajaxForm', 'POST', _link("admin/basic/api/dictionary.json"), 'return false', 'multipart/form-data'); $form->setHandleBySelf(1); $form->setCallback("(function(msg){if(msg.code == 0){trz.popup.confirm('操作成功', {confirmFunc:function(){window.location.href='". _link("admin/basic/ad.html") ."'}});}else{trz.popup.alert(msg.msg||'操作失败')}})(msg)"); $form->addHidden('', 'dictionary_value[app_id]', ['value' => $appID]); $form->addHidden('', 'dictionary_key', ['value' => 'ad_'. $appID]); $form->addHidden('', 'isEdit', ['value' => $isEdit]); $form->addRadio('是否启用广告功能', 'dictionary_value[enable]', [ 'default' => $info['enable'] ?? 0, 'option' => [ ['是', 1], ['否', 0] ] ]); $form->addInputs([ ['跳转地址', 'dictionary_value[url]', ['tips' => '* 点击广告跳转地址', 'value' => $info['url'] ?? '']], ['某一章节显示', 'dictionary_value[show_chapter_number]', ['tips' => '* 指定固定章节显示', 'value' => $info['url'] ?? '']], ['多章节显示', 'dictionary_value[show_chapter_number_rules]', ['tips' => '* 基于章节规则显示,如 $x > 50 && $x % 50 == 2 || $x % 50 == 4', 'value' => $info['show_chapter_number_rules'] ?? '']], ['章节倍数显示', 'dictionary_value[show_chapter_number_times]', ['tips' => '* 章节倍数展示,如:10的倍数 = $x % 10 = 0 则显示', 'value' => $info['show_chapter_number_times'] ?? '']] ]); $form->addFile('广告图片/视频', 'dictionary_value[file]', ['size' => '50M', 'extensions' => 'png gif jpeg jpg mp4 m3u8', 'tips' => '图片文件支持png jpg gif jpeg , 视频支持mp4 m3u8,最大50M', 'value' => _link($info['file'] ?? '')]); //设置默认值来规避file不选择上传文件,之前上传的文件字段被清空问题 $form->addHidden('', '__defaults[file]', ['value' => $info['file'] ?? '']); $form->addTextarea('广告代码', 'dictionary_value[ad_code]', ['value' => $info['ad_code'] ?? '']); $this->view->assign('form', $form);
模版里边使用,异步提交参见表单验证并提交JS:
{#$form ->appendHtml('<button type="submit" name="提交" class="btn btn-info waves-effect waves-light btn-ajaxForm">提交</button>') ->render()#}
没有帐号? 现在注册.