admin管理员组

文章数量:1203209

My form has a name input and an alias input. They are in a Primevue Form inside a FormField and both have a resolver that checks for required input.

When a name is entered in the name field the value is being copied by the program to the alias field. In this case the alias resolver isn't being called and the alias field shows an error 'required' message.

When entering a value manually in the input the resolver is being called and the 'required' message appears or disappears depending on alias having a value or not.

Question is when setting values programmatically how can I trigger the validation to be performed that is set on the respective FormFields?

My form has a name input and an alias input. They are in a Primevue Form inside a FormField and both have a resolver that checks for required input.

When a name is entered in the name field the value is being copied by the program to the alias field. In this case the alias resolver isn't being called and the alias field shows an error 'required' message.

When entering a value manually in the input the resolver is being called and the 'required' message appears or disappears depending on alias having a value or not.

Question is when setting values programmatically how can I trigger the validation to be performed that is set on the respective FormFields?

Share Improve this question edited Jan 22 at 7:47 DarkBee 15.6k8 gold badges70 silver badges114 bronze badges asked Jan 22 at 7:37 ReneRene 10.5k5 gold badges36 silver badges48 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Found a way to trigger Primevue FormField validation after setting the input value programmatically.

Give the FormField a ref property:

<FormField ref="alias"

In the code where you set the value of the alias field you can now reference the internal state values of the input. These need to be changed to trigger validation.

 $_setAlias() {
            this.record.alias = this.record.name;                   // Set alias equal to the name of the record
            this.$refs.news.field.states.dirty = true;              // Set some internal status fields
            this.$refs.alias.field.states.touched = true;            
            this.$refs.alias.field.states.pristine = false;          
            this.$refs.alias.field.states.value = this.record.title; // Set internal field value.
        },

With these settings field validation is triggered. Haven't figured out if setting only some of these values works as well. That needs further investigation.

本文标签: primevueHow to validate input when value is set programmatically in form validationStack Overflow