## **表單生成**
* 當需要對WEB項目數據輸入時,就需要使用到表單元素,通過網頁將填寫好的信息發送到后端處理后保存至數據庫。官方手冊:https://getbootstrap.com/docs/4.5/components/forms/
* 在繼承基類模板后,模板里統一使用助手函數 ```DcBuildForm``` 來生成表單,如果您沒有繼承基類模板,則還需要在網頁里調用表單事件監聽方法daicuo.form.submit()。
```
{:DcBuildForm($args)}
```
## **表單屬性**
| 參數 | 類型 | 必須 | 默認 | 說明 |
|------|--------|----|----|------|
| action | string | y | | 表單提交地址 |
| class | string | n | | 表單class屬性<br>form-inline<br>was-validated |
| method | string | n | post | 表單提交方式 |
| ajax | bool | y | true | AJAX提交表單 |
| submit | string | n | 空 | 提交按鈕 |
| close | string | n | 空 | 關閉按鈕 |
| reset | string | n | 空 | 重置按鈕 |
| disabled | bool | n | false | 是否禁止提交 |
| callback | string | n | function | AJAX提交成功后的回調方法 |
| items | array | y | 數組 | 表單控件列表,參考下文 |
## **示例代碼1**
```
{:DcBuildForm([
'name' => 'daohang_config',
'class' => 'bg-white px-2 py-2',
'action' => DcUrlAddon(['module'=>'daohang','controll'=>'admin','action'=>'update'],''),
'method' => 'post',
'ajax' => true,
'submit' => lang('submit'),
'reset' => lang('reset'),
'close' => false,
'disabled' => false,
'callback' => '',
'items' => DcFormItems([
'site_theme' => ['type'=>'select', 'value'=>config('site_theme'), 'option'=>$optionThemes],
'theme_wap' => ['type'=>'select', 'value'=>config('theme_wap'), 'option'=>$optionThemes],
'url_type' => ['type'=>'select', 'value'=>config('daohang.url_type'), 'option'=>$optionUrlType],
'url_index' => ['type'=>'text', 'value'=>config('daohang.url_index')],
'url_category' => ['type'=>'text', 'value'=>config('daohang.url_category')],
'url_tag' => ['type'=>'text', 'value'=>config('daohang.url_tag')],
'url_search' => ['type'=>'text', 'value'=>config('daohang.url_search')],
'url_detail' => ['type'=>'text', 'value'=>config('daohang.url_detail')],
'page_size' => ['type'=>'number', 'value'=>config('daohang.page_size')],
'title_index' => ['type'=>'text', 'value'=>config('daohang.title_index')],
'keywords_index' => ['type'=>'text', 'value'=>config('daohang.keywords_index')],
'description_index' => ['type'=>'text', 'value'=>config('daohang.description_index')],
])
])}
```
## **示例代碼2**
與示例代碼1不同的是items參數手動添加,不是通過DcFormItems函數生成。
```
{:DcBuildForm([
'class'=>'bg-white px-2 py-2',
'action'=>DcUrl('admin/video/update', 'module=common', ''),
'method'=>'post',
'ajax'=>true,
'submit'=>lang('submit'),
'close'=>lang('close'),
'reset'=>lang('reset'),
'disabled'=>false,
'callback'=>'daicuo.form.test',
'items'=>[
[
'type'=>'switch',
'name'=>'video_in',
'id'=>'video_in',
'title'=>lang('video_in'),
'tips'=>'',
'value'=>config('common.video_in'),
'readonly'=>false,
'disabled'=>false,
'required'=>false,
'class'=>'row form-group',
'class_left'=>'col-md-2',
'class_right'=>'col-auto',
'class_right_control'=>'',
'class_right_tips'=>'',
],
[
'type'=>'select.custom',
'name'=>'video_size',
'id'=>'video_size',
'title'=>lang('video_size'),
'placeholder'=>'',
'tips'=>'',
'value'=>config('common.video_size'),
'option'=>['16by9'=>lang('video_size_16by9'),'21by9'=>lang('video_size_21by9'),'4by3'=>lang('video_size_4by3'),'1by1'=>lang('video_size_1by1')],
'readonly'=>false,
'disabled'=>false,
'required'=>false,
'class'=>'row form-group',
'class_left'=>'col-md-2',
'class_right'=>'col-auto',
'class_right_control'=>'',
'class_right_tips'=>'',
],
[
'type'=>'text',
'name'=>'video_ai',
'id'=>'video_ai',
'title'=>lang('video_ai'),
'placeholder'=>lang('video_ai_placeholder'),
'tips'=>'',
'value'=>config('common.video_ai'),
'readonly'=>false,
'disabled'=>false,
'required'=>false,
'class'=>'row form-group',
'class_left'=>'col-md-2',
'class_right'=>'col-md-6',
'class_right_control'=>'',
'class_right_tips'=>'',
]
])}
```
**callback JS方法的參數**
| 參數 | 類型 | 必須 | 默認 | 說明 |
|------|--------|----|----|------|
| data | obj | y | | ajax返回的數據 |
| status | string | y | | ajax返回的狀態 |
| xhr | string | y | | jquery的xhr裝態 |
**callback 示例代碼**
```
function callback(data,status,xhr){
......
}
```