index.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <template>
  2. <div class="form-com">
  3. <el-form ref="form" :rules="rules" :model="form" :label-width="configs.labelWidth||'80px'" :label-position="configs.labelPosition||'right'">
  4. <el-collapse v-if="isGroup" v-model="activeNames">
  5. <el-collapse-item v-for="(item, i) in configs.fields" :key="i" :title="item.groupTitle" :name="i">
  6. <Form :form.sync="form" :fields.sync="item.children" :columnNum="configs.columnNum">
  7. <template v-for="(item, i) in $scopedSlots" :slot="i" slot-scope="scope">
  8. <!-- <template v-if="hasSlot(item.children, i)" > -->
  9. <slot :name="i" :scope="scope"/>
  10. <!-- </template> -->
  11. </template>
  12. </Form>
  13. </el-collapse-item>
  14. </el-collapse>
  15. <template v-else>
  16. <Form :form.sync="form" :fields.sync="configs.fields" :columnNum="configs.columnNum">
  17. <template v-for="(item, i) in $scopedSlots" :slot="i" slot-scope="scope">
  18. <!-- <template v-if="hasSlot(item.children, i)" > -->
  19. <slot :name="i" :scope="scope"/>
  20. <!-- </template> -->
  21. </template>
  22. </Form>
  23. </template>
  24. </el-form>
  25. <div class="pt50 tc">
  26. <slot name="footer"></slot>
  27. </div>
  28. <!-- <div class="tc pt50">
  29. <el-button >取消</el-button>
  30. <el-button type="primary" @click="submit()">保存</el-button>
  31. </div> -->
  32. </div>
  33. </template>
  34. <script>
  35. import Form from './form'
  36. import {isIdcard} from '../../utils/validate'
  37. import validateForm from '../../utils/validateForm'
  38. var validateMobile = (rule, value, callback) => {
  39. if(!(/^1[3456789]\d{9}$/.test(value))){
  40. return callback(new Error('号码有误,请重填'));
  41. }
  42. return callback()
  43. };
  44. var validateIdCard = (rule, value, callback) => {
  45. if(!value){
  46. return callback();
  47. }
  48. var idcard = value
  49. var idcardLen = idcard.length,
  50. xchar = idcard.charAt(idcardLen-1);
  51. if (xchar === 'x') {
  52. idcard = idcard.substr(0, idcardLen-1) + 'X';
  53. }
  54. if(!isIdcard(idcard)){
  55. return callback(new Error('号码有误,请重填'));
  56. }
  57. return callback()
  58. };
  59. export default {
  60. name: 'CustomForm',
  61. props: {
  62. 'configs':{},
  63. },
  64. components:{
  65. Form
  66. },
  67. computed:{
  68. isGroup(){
  69. return !!this.configs.fields[0].groupTitle
  70. }
  71. },
  72. watch:{
  73. form:{
  74. handler(n){
  75. this.$emit('input', this.form)
  76. },
  77. deep: true
  78. }
  79. },
  80. data(){
  81. return {
  82. activeNames: [],
  83. form: "",
  84. rules: ""
  85. }
  86. },
  87. created(){
  88. if(this.isGroup){
  89. var activeNames = []
  90. this.configs.fields.forEach((v, i) => {
  91. activeNames.push(i)
  92. });
  93. this.activeNames = activeNames
  94. }
  95. var form = {}
  96. var rules = {}
  97. this.configs.fields.forEach((v, i) => {
  98. if(v.groupTitle){
  99. v.children.forEach((m, n)=>{
  100. form[m.id] = m.value||m.value===0||m.value===false? m.value : m.type=='checkbox'? [] :""
  101. rules[m.id] = this.setRule(m)
  102. })
  103. } else {
  104. form[v.id] = v.value||v.value===0||v.value===false? v.value : v.type=='checkbox'? [] :""
  105. rules[v.id] = this.setRule(v)
  106. }
  107. });
  108. this.form = form
  109. this.rules = rules
  110. console.log(this.rules)
  111. },
  112. mounted() {
  113. },
  114. methods: {
  115. setRule(item){
  116. if(item.rules){
  117. return item.rules
  118. }
  119. var r = []
  120. if(item.required){
  121. r.push({
  122. required: true,
  123. message: "该项必填",
  124. trigger: "blur"
  125. })
  126. }
  127. if(item.regTel){
  128. //手机号码
  129. r.push({
  130. validator: validateMobile,
  131. trigger: 'blur'
  132. })
  133. }
  134. if(item.regIdcard){
  135. //身份证
  136. r.push({
  137. validator: validateIdCard,
  138. trigger: 'blur'
  139. })
  140. }
  141. if(item.type == 'digit'){
  142. r.push({
  143. validator: validateForm['digit'],
  144. trigger: 'blur'
  145. })
  146. }
  147. if(item.regularType && validateForm[item.regularType]){
  148. r.push({
  149. validator: validateForm[item.regularType],
  150. trigger: 'blur'
  151. })
  152. }
  153. return r
  154. },
  155. submit() {
  156. var vm = this;
  157. return new Promise((resolve, reject)=>{
  158. this.$refs['form'].validate(valid => {
  159. if(valid){
  160. resolve(this.form)
  161. } else {
  162. resolve(valid)
  163. }
  164. });
  165. })
  166. },
  167. setValues(model){
  168. for(var k in model){
  169. this.form[k] = model[k]
  170. }
  171. },
  172. setValue(id, value){
  173. this.form[id] = value
  174. },
  175. hasSlot(list, key){
  176. for(var i=0; i<list.length; i++){
  177. if(list[i].id == key){
  178. return true
  179. }
  180. }
  181. return false
  182. },
  183. setOptions(configs, key, options){
  184. configs.fields.forEach(v=>{
  185. if(v.children){
  186. v.children.forEach(m=>{
  187. if(m.id == key){
  188. m.optionList = options
  189. }
  190. })
  191. } else {
  192. if(v.id == key){
  193. v.optionList = options
  194. }
  195. }
  196. })
  197. }
  198. }
  199. }
  200. </script>
  201. <style lang="scss">
  202. .form-com{
  203. .el-collapse-item__header{
  204. font-size: 16px;
  205. }
  206. .el-collapse-item__content{
  207. padding-bottom: 0;
  208. padding-top: 10px;
  209. }
  210. }
  211. </style>