提交 a988eb1c 编写于 作者: zcdtk's avatar zcdtk

formitem

代码格式优化
上级 ae068cae
...@@ -73,7 +73,7 @@ export default class AppFormItem extends Vue { ...@@ -73,7 +73,7 @@ export default class AppFormItem extends Vue {
* @type {string} * @type {string}
* @memberof AppFormItem * @memberof AppFormItem
*/ */
@Prop() public checkValue?: any; @Prop() public itemValue?: any;
/** /**
* 校验值规则 * 校验值规则
...@@ -81,37 +81,9 @@ export default class AppFormItem extends Vue { ...@@ -81,37 +81,9 @@ export default class AppFormItem extends Vue {
* @type {string} * @type {string}
* @memberof AppFormItem * @memberof AppFormItem
*/ */
@Watch('checkValue') @Watch('itemValue')
checkValueChange(){ onItemValueChange() {
this.checkRule(); this.validateRules();
}
public checkRule(): boolean {
let _this = this
let erro_text :any = "";
let isCheck :boolean = true;
let required :boolean = true;
this.rules.forEach((item:any)=>{
if(item.hasOwnProperty('pattern') && item.trigger == 'change' && _this.checkValue){
let pattern: any = item.pattern;
if(!pattern.test( _this.checkValue)){
erro_text = item.message;
isCheck = false;
}
}
if(item.hasOwnProperty('required') && item.trigger == 'change'){
if(item.required && ! _this.checkValue){
erro_text = item.message;
required = false;
}
}
});
this.errorText = erro_text;
if(isCheck && required){
return true
}else{
return false
}
} }
/** /**
...@@ -120,8 +92,7 @@ export default class AppFormItem extends Vue { ...@@ -120,8 +92,7 @@ export default class AppFormItem extends Vue {
* @type {string} * @type {string}
* @memberof AppFormItem * @memberof AppFormItem
*/ */
public errorText = ""; public errorText: string = '';
/** /**
* label样式 * label样式
...@@ -210,23 +181,12 @@ export default class AppFormItem extends Vue { ...@@ -210,23 +181,12 @@ export default class AppFormItem extends Vue {
* @param {*} oldVal * @param {*} oldVal
* @memberof AppFormItem * @memberof AppFormItem
*/ */
@Watch('itemRules',{deep:true}) @Watch('itemRules', { deep: true })
onItemRulesChange(newVal: any, oldVal: any) { onItemRulesChange(newVal: any, oldVal: any) {
if (newVal) { if (!newVal) {
try { return;
this.rules = [];
const _rules: any[] = newVal;
this.rules = [..._rules];
this.rules.some((rule: any) => {
if (rule.hasOwnProperty('required')) {
this.required = rule.required;
return true;
}
return false;
});
} catch (error) {
}
} }
this.computeRequired(newVal);
} }
...@@ -265,23 +225,64 @@ export default class AppFormItem extends Vue { ...@@ -265,23 +225,64 @@ export default class AppFormItem extends Vue {
*/ */
public mounted() { public mounted() {
if (this.itemRules) { if (this.itemRules) {
try { this.computeRequired(this.itemRules);
const _rules: any[] = this.itemRules; }
this.rules = [..._rules]; }
this.rules.some((rule: any) => {
if (rule.hasOwnProperty('required')) { /**
this.required = rule.required; * 计算是否必填
return true; *
} * @private
return false; * @param {*} rules
}); * @memberof AppFormItem
} catch (error) { */
} private computeRequired(rules: any): void {
try {
const _rules: any[] = rules;
this.rules = [..._rules];
this.rules.some((rule: any) => {
if (rule.hasOwnProperty('required')) {
this.required = rule.required;
return true;
}
return false;
});
} catch (error) {
} }
} }
/**
* 校验值规则
*
* @returns {boolean}
* @memberof AppFormItem
*/
public validateRules(): boolean {
let erro_text: any = "";
let isCheck: boolean = true;
let required: boolean = true;
this.rules.forEach((item: any) => {
if (item.hasOwnProperty('pattern') && item.trigger == 'change' && this.itemValue) {
let pattern: any = item.pattern;
if (!pattern.test(this.itemValue)) {
erro_text = item.message;
isCheck = false;
}
}
if (item.hasOwnProperty('required') && item.trigger == 'change') {
if (item.required && !this.itemValue) {
erro_text = item.message;
required = false;
}
}
});
this.errorText = erro_text;
return isCheck && required ? true : false;
}
} }
</script> </script>
<style lang='less'> <style lang='less'>
@import './app-form-item.less'; @import './app-form-item.less';
</style> </style>
\ No newline at end of file
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册