feat: uid validate (#681)
This commit is contained in:
		
							parent
							
								
									d9b2bf8af1
								
							
						
					
					
						commit
						827c324be3
					
				| @ -1,4 +1,9 @@ | ||||
| import { registerValidateFormats } from '@formily/core'; | ||||
| 
 | ||||
| export * from './AddFieldAction'; | ||||
| export * from './ConfigurationTable'; | ||||
| export * from './EditFieldAction'; | ||||
| 
 | ||||
| registerValidateFormats({ | ||||
|   uid: /^[A-Za-z0-9][A-Za-z0-9_-]*$/, | ||||
| }); | ||||
|  | ||||
| @ -1,8 +1,5 @@ | ||||
| import { ISchema, Schema } from '@formily/react'; | ||||
| import { useTranslation } from 'react-i18next'; | ||||
| import { i18n } from '../../../i18n'; | ||||
| import { useRecord } from '../../../record-provider'; | ||||
| import { useCompile } from '../../../schema-component'; | ||||
| import { CollectionOptions } from '../../types'; | ||||
| import { collectionFieldSchema } from './collectionFields'; | ||||
| 
 | ||||
| @ -121,6 +118,7 @@ export const collectionSchema: ISchema = { | ||||
|                     name: { | ||||
|                       'x-component': 'CollectionField', | ||||
|                       'x-decorator': 'FormItem', | ||||
|                       'x-validator': 'uid', | ||||
|                     }, | ||||
|                     footer: { | ||||
|                       type: 'void', | ||||
|  | ||||
| @ -175,6 +175,7 @@ export const m2m: IField = { | ||||
|                     "{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}", | ||||
|                   'x-decorator': 'FormItem', | ||||
|                   'x-component': 'Input', | ||||
|                   'x-validator': 'uid', | ||||
|                   'x-disabled': '{{ !createOnly }}', | ||||
|                 }, | ||||
|               }, | ||||
| @ -208,6 +209,7 @@ export const m2m: IField = { | ||||
|                     "{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}", | ||||
|                   'x-decorator': 'FormItem', | ||||
|                   'x-component': 'Input', | ||||
|                   'x-validator': 'uid', | ||||
|                   'x-disabled': '{{ !createOnly }}', | ||||
|                 }, | ||||
|               }, | ||||
|  | ||||
| @ -181,6 +181,7 @@ export const m2o: IField = { | ||||
|                     "{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}", | ||||
|                   'x-decorator': 'FormItem', | ||||
|                   'x-component': 'Input', | ||||
|                   'x-validator': 'uid', | ||||
|                   'x-disabled': '{{ !createOnly }}', | ||||
|                 }, | ||||
|               }, | ||||
|  | ||||
| @ -207,6 +207,7 @@ export const o2m: IField = { | ||||
|                     "{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}", | ||||
|                   'x-decorator': 'FormItem', | ||||
|                   'x-component': 'Input', | ||||
|                   'x-validator': 'uid', | ||||
|                   'x-disabled': '{{ !createOnly }}', | ||||
|                 }, | ||||
|               }, | ||||
|  | ||||
| @ -198,6 +198,7 @@ export const o2o: IField = { | ||||
|                     "{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}", | ||||
|                   'x-decorator': 'FormItem', | ||||
|                   'x-component': 'Input', | ||||
|                   'x-validator': 'uid', | ||||
|                   'x-disabled': '{{ !createOnly }}', | ||||
|                 }, | ||||
|               }, | ||||
| @ -362,6 +363,7 @@ export const oho: IField = { | ||||
|                     "{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}", | ||||
|                   'x-decorator': 'FormItem', | ||||
|                   'x-component': 'Input', | ||||
|                   'x-validator': 'uid', | ||||
|                   'x-disabled': '{{ !createOnly }}', | ||||
|                 }, | ||||
|               }, | ||||
| @ -513,6 +515,7 @@ export const obo: IField = { | ||||
|                     "{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}", | ||||
|                   'x-decorator': 'FormItem', | ||||
|                   'x-component': 'Input', | ||||
|                   'x-validator': 'uid', | ||||
|                   'x-disabled': '{{ !createOnly }}', | ||||
|                 }, | ||||
|               }, | ||||
|  | ||||
| @ -218,6 +218,7 @@ export const defaultProps = { | ||||
|     'x-disabled': '{{ !createOnly }}', | ||||
|     'x-decorator': 'FormItem', | ||||
|     'x-component': 'Input', | ||||
|     'x-validator': 'uid', | ||||
|     description: | ||||
|       "{{t('Randomly generated and can be modified. Support letters, numbers and underscores, must start with an letter.')}}", | ||||
|   }, | ||||
|  | ||||
| @ -8,17 +8,35 @@ export class UidField extends Field { | ||||
|   } | ||||
| 
 | ||||
|   init() { | ||||
|     const { name, prefix = '' } = this.options; | ||||
|     const { model } = this.context.collection; | ||||
|     model.beforeCreate(async (instance) => { | ||||
|       if (!instance.get(name)) { | ||||
|     const { name, prefix = '', pattern } = this.options; | ||||
|     const re = new RegExp(pattern || '^[A-Za-z0-9][A-Za-z0-9_-]*$'); | ||||
|     this.listener = async (instance) => { | ||||
|       const value = instance.get(name); | ||||
|       if (!value) { | ||||
|         instance.set(name, `${prefix}${uid()}`); | ||||
|       } else if (re.test(value)) { | ||||
|         instance.set(name, value); | ||||
|       } else { | ||||
|         throw new Error(`uid '${value}' is invalid`); | ||||
|       } | ||||
|     }); | ||||
|     }; | ||||
|   } | ||||
| 
 | ||||
|   bind() { | ||||
|     super.bind(); | ||||
|     this.on('beforeCreate', this.listener); | ||||
|     this.on('beforeUpdate', this.listener); | ||||
|   } | ||||
| 
 | ||||
|   unbind() { | ||||
|     super.unbind(); | ||||
|     this.off('beforeCreate', this.listener); | ||||
|     this.off('beforeUpdate', this.listener); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| export interface UidFieldOptions extends BaseColumnFieldOptions { | ||||
|   type: 'uid'; | ||||
|   prefix?: string; | ||||
|   pattern?: string; | ||||
| } | ||||
|  | ||||
| @ -37,6 +37,7 @@ export default { | ||||
|         type: 'string', | ||||
|         title: '{{t("Email")}}', | ||||
|         'x-component': 'Input', | ||||
|         'x-validator': 'email', | ||||
|         require: true, | ||||
|       }, | ||||
|     }, | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user