|
@@ -120,78 +120,46 @@
|
|
|
{ validator: checkAge, trigger: 'blur' }
|
|
|
]
|
|
|
},
|
|
|
- dynamicForm: {
|
|
|
+ dynamicValidateForm: {
|
|
|
domains: [{
|
|
|
key: Date.now(),
|
|
|
value: ''
|
|
|
}],
|
|
|
email: ''
|
|
|
},
|
|
|
- dynamicRule: {
|
|
|
- email: [
|
|
|
- { required: true, message: 'Please input email address', trigger: 'blur' },
|
|
|
- { type: 'email', message: 'Please input correct email address', trigger: 'blur,change' }
|
|
|
- ]
|
|
|
+ numberValidateForm: {
|
|
|
+ age: ''
|
|
|
}
|
|
|
};
|
|
|
},
|
|
|
methods: {
|
|
|
- handleSubmit(ev) {
|
|
|
- this.$refs.ruleForm.validate((valid) => {
|
|
|
- if (valid) {
|
|
|
- alert('submit!');
|
|
|
- } else {
|
|
|
- console.log('error submit!!');
|
|
|
- return false;
|
|
|
- }
|
|
|
- });
|
|
|
+ onSubmit() {
|
|
|
+ console.log('submit!');
|
|
|
},
|
|
|
- handleSubmit2(ev) {
|
|
|
- this.$refs.ruleForm2.validate(valid => {
|
|
|
- if (valid) {
|
|
|
- alert('Submit');
|
|
|
- } else {
|
|
|
- console.log('error submit!!');
|
|
|
- return false;
|
|
|
- }
|
|
|
- });
|
|
|
+ onRuleFormSubmit() {
|
|
|
+ console.log('onRuleFormSubmit');
|
|
|
},
|
|
|
- handleSubmit3(ev) {
|
|
|
- this.$refs.dynamicForm.validate(valid => {
|
|
|
+ submitForm(formName) {
|
|
|
+ this.$refs[formName].validate((valid) => {
|
|
|
if (valid) {
|
|
|
- alert('Submit');
|
|
|
+ alert('submit!');
|
|
|
} else {
|
|
|
console.log('error submit!!');
|
|
|
return false;
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
- handleReset() {
|
|
|
- this.$refs.ruleForm.resetFields();
|
|
|
- },
|
|
|
- handleReset2() {
|
|
|
- this.$refs.ruleForm2.resetFields();
|
|
|
- },
|
|
|
- handleReset3() {
|
|
|
- this.$refs.dynamicForm.resetFields();
|
|
|
- },
|
|
|
- handleValidate(prop, errorMsg) {
|
|
|
- console.log(prop, errorMsg);
|
|
|
- },
|
|
|
- onSubmit() {
|
|
|
- console.log('submit!');
|
|
|
- },
|
|
|
- onRuleFormSubmit() {
|
|
|
- console.log('onRuleFormSubmit');
|
|
|
+ resetForm(formName) {
|
|
|
+ this.$refs[formName].resetFields();
|
|
|
},
|
|
|
removeDomain(item) {
|
|
|
- var index = this.dynamicForm.domains.indexOf(item)
|
|
|
+ var index = this.dynamicValidateForm.domains.indexOf(item)
|
|
|
if (index !== -1) {
|
|
|
- this.dynamicForm.domains.splice(index, 1)
|
|
|
+ this.dynamicValidateForm.domains.splice(index, 1)
|
|
|
}
|
|
|
},
|
|
|
addDomain() {
|
|
|
- this.dynamicForm.domains.push({
|
|
|
+ this.dynamicValidateForm.domains.push({
|
|
|
key: Date.now(),
|
|
|
value: ''
|
|
|
});
|
|
@@ -552,8 +520,8 @@ Form component allows you to verify your data, helping you find and correct erro
|
|
|
<el-input type="textarea" v-model="ruleForm.desc"></el-input>
|
|
|
</el-form-item>
|
|
|
<el-form-item>
|
|
|
- <el-button type="primary" @click="handleSubmit">Create</el-button>
|
|
|
- <el-button @click="handleReset">Reset</el-button>
|
|
|
+ <el-button type="primary" @click="submitForm('ruleForm')">Create</el-button>
|
|
|
+ <el-button @click="resetForm('ruleForm')">Reset</el-button>
|
|
|
</el-form-item>
|
|
|
</el-form>
|
|
|
<script>
|
|
@@ -597,11 +565,8 @@ Form component allows you to verify your data, helping you find and correct erro
|
|
|
};
|
|
|
},
|
|
|
methods: {
|
|
|
- handleReset() {
|
|
|
- this.$refs.ruleForm.resetFields();
|
|
|
- },
|
|
|
- handleSubmit(ev) {
|
|
|
- this.$refs.ruleForm.validate((valid) => {
|
|
|
+ submitForm(formName) {
|
|
|
+ this.$refs[formName].validate((valid) => {
|
|
|
if (valid) {
|
|
|
alert('submit!');
|
|
|
} else {
|
|
@@ -609,6 +574,9 @@ Form component allows you to verify your data, helping you find and correct erro
|
|
|
return false;
|
|
|
}
|
|
|
});
|
|
|
+ },
|
|
|
+ resetForm(formName) {
|
|
|
+ this.$refs[formName].resetFields();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -632,8 +600,8 @@ Form component allows you to verify your data, helping you find and correct erro
|
|
|
<el-input v-model.number="ruleForm2.age"></el-input>
|
|
|
</el-form-item>
|
|
|
<el-form-item>
|
|
|
- <el-button type="primary" @click="handleSubmit2">Submit</el-button>
|
|
|
- <el-button @click="handleReset2">Reset</el-button>
|
|
|
+ <el-button type="primary" @click="submitForm('ruleForm2')">Submit</el-button>
|
|
|
+ <el-button @click="resetForm('ruleForm2')">Reset</el-button>
|
|
|
</el-form-item>
|
|
|
</el-form>
|
|
|
<script>
|
|
@@ -694,11 +662,8 @@ Form component allows you to verify your data, helping you find and correct erro
|
|
|
};
|
|
|
},
|
|
|
methods: {
|
|
|
- handleReset2() {
|
|
|
- this.$refs.ruleForm2.resetFields();
|
|
|
- },
|
|
|
- handleSubmit2(ev) {
|
|
|
- this.$refs.ruleForm2.validate((valid) => {
|
|
|
+ submitForm(formName) {
|
|
|
+ this.$refs[formName].validate((valid) => {
|
|
|
if (valid) {
|
|
|
alert('submit!');
|
|
|
} else {
|
|
@@ -706,6 +671,9 @@ Form component allows you to verify your data, helping you find and correct erro
|
|
|
return false;
|
|
|
}
|
|
|
});
|
|
|
+ },
|
|
|
+ resetForm(formName) {
|
|
|
+ this.$refs[formName].resetFields();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -718,12 +686,19 @@ Form component allows you to verify your data, helping you find and correct erro
|
|
|
:::demo In addition to passing all validation rules at once on the form component, you can also pass the validation rules or delete rules on a single form field dynamically.
|
|
|
|
|
|
```html
|
|
|
-<el-form :model="dynamicForm" :rules="dynamicRule" ref="dynamicForm" label-width="120px" class="demo-dynamic">
|
|
|
- <el-form-item prop="email" label="Email">
|
|
|
- <el-input v-model="dynamicForm.email"></el-input>
|
|
|
+<el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="120px" class="demo-dynamic">
|
|
|
+ <el-form-item
|
|
|
+ prop="email"
|
|
|
+ label="Email"
|
|
|
+ :rules="[
|
|
|
+ { required: true, message: 'Please input email address', trigger: 'blur' },
|
|
|
+ { type: 'email', message: 'Please input correct email address', trigger: 'blur,change' }
|
|
|
+ ]"
|
|
|
+ >
|
|
|
+ <el-input v-model="dynamicValidateForm.email"></el-input>
|
|
|
</el-form-item>
|
|
|
<el-form-item
|
|
|
- v-for="(domain, index) in dynamicForm.domains"
|
|
|
+ v-for="(domain, index) in dynamicValidateForm.domains"
|
|
|
:label="'Domain' + index"
|
|
|
:key="domain.key"
|
|
|
:prop="'domains.' + index + '.value'"
|
|
@@ -734,33 +709,27 @@ Form component allows you to verify your data, helping you find and correct erro
|
|
|
<el-input v-model="domain.value"></el-input><el-button @click.prevent="removeDomain(domain)">Delete</el-button>
|
|
|
</el-form-item>
|
|
|
<el-form-item>
|
|
|
- <el-button type="primary" @click="handleSubmit3">Submit</el-button>
|
|
|
+ <el-button type="primary" @click="submitForm('dynamicValidateForm')">Submit</el-button>
|
|
|
<el-button @click="addDomain">New domain</el-button>
|
|
|
- <el-button @click="handleReset3">Reset</el-button>
|
|
|
+ <el-button @click="resetForm('dynamicValidateForm')">Reset</el-button>
|
|
|
</el-form-item>
|
|
|
</el-form>
|
|
|
<script>
|
|
|
export default {
|
|
|
data() {
|
|
|
return {
|
|
|
- dynamicForm: {
|
|
|
+ dynamicValidateForm: {
|
|
|
domains: [{
|
|
|
key: 1,
|
|
|
value: ''
|
|
|
}],
|
|
|
email: ''
|
|
|
- },
|
|
|
- dynamicRule: {
|
|
|
- email: [
|
|
|
- { required: true, message: 'Please input email address', trigger: 'blur' },
|
|
|
- { type: 'email', message: 'Please input correct email address', trigger: 'blur,change' }
|
|
|
- ]
|
|
|
}
|
|
|
};
|
|
|
},
|
|
|
methods: {
|
|
|
- handleSubmit3(ev) {
|
|
|
- this.$refs.dynamicForm.validate((valid) => {
|
|
|
+ submitForm(formName) {
|
|
|
+ this.$refs[formName].validate((valid) => {
|
|
|
if (valid) {
|
|
|
alert('submit!');
|
|
|
} else {
|
|
@@ -769,17 +738,17 @@ Form component allows you to verify your data, helping you find and correct erro
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
- handleReset3() {
|
|
|
- this.$refs.dynamicForm.resetFields();
|
|
|
+ resetForm(formName) {
|
|
|
+ this.$refs[formName].resetFields();
|
|
|
},
|
|
|
removeDomain(item) {
|
|
|
- var index = this.dynamicForm.domains.indexOf(item);
|
|
|
+ var index = this.dynamicValidateForm.domains.indexOf(item);
|
|
|
if (index !== -1) {
|
|
|
- this.dynamicForm.domains.splice(index, 1);
|
|
|
+ this.dynamicValidateForm.domains.splice(index, 1);
|
|
|
}
|
|
|
},
|
|
|
addDomain() {
|
|
|
- this.dynamicForm.domains.push({
|
|
|
+ this.dynamicValidateForm.domains.push({
|
|
|
key: Date.now(),
|
|
|
value: ''
|
|
|
});
|
|
@@ -788,7 +757,56 @@ Form component allows you to verify your data, helping you find and correct erro
|
|
|
}
|
|
|
</script>
|
|
|
```
|
|
|
-:::
|
|
|
+:::
|
|
|
+
|
|
|
+### Number Validate
|
|
|
+
|
|
|
+::: demo Number Validate need a `.number` modifier added on the input `v-model` binding,it's used to transform the string value to the number which is provided by Vuejs.
|
|
|
+```html
|
|
|
+<el-form :model="numberValidateForm" ref="numberValidateForm" label-width="100px" class="demo-ruleForm">
|
|
|
+ <el-form-item
|
|
|
+ label="age"
|
|
|
+ prop="age"
|
|
|
+ :rules="[
|
|
|
+ { required: true, message: 'age is required'},
|
|
|
+ { type: 'number', message: 'age must be a number'}
|
|
|
+ ]"
|
|
|
+ >
|
|
|
+ <el-input type="age" v-model.number="numberValidateForm.age" auto-complete="off"></el-input>
|
|
|
+ </el-form-item>
|
|
|
+ <el-form-item>
|
|
|
+ <el-button type="primary" @click="submitForm('numberValidateForm')">Submit</el-button>
|
|
|
+ <el-button @click="resetForm('numberValidateForm')">Reset</el-button>
|
|
|
+ </el-form-item>
|
|
|
+</el-form>
|
|
|
+<script>
|
|
|
+ export default {
|
|
|
+ data() {
|
|
|
+ return {
|
|
|
+ numberValidateForm: {
|
|
|
+ age: ''
|
|
|
+ }
|
|
|
+ };
|
|
|
+ },
|
|
|
+ methods: {
|
|
|
+ submitForm(formName) {
|
|
|
+ this.$refs[formName].validate((valid) => {
|
|
|
+ if (valid) {
|
|
|
+ alert('submit!');
|
|
|
+ } else {
|
|
|
+ console.log('error submit!!');
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ },
|
|
|
+ resetForm(formName) {
|
|
|
+ this.$refs[formName].resetFields();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+</script>
|
|
|
+```
|
|
|
+:::
|
|
|
|
|
|
### Form Attributes
|
|
|
|