The form validation feature in Element UI is having issues when used with the Vue 2 Composition API

I am currently developing a form that utilizes element UI's form validation within the vue 2 composition API environment.

This form includes a table nested inside, making its structure somewhat complex.

...
<div>
    <el-form
      ref="smKeyInfoForm"
      :model="formData"
      :rules="formRules">
      <el-table
        :data="tableData"
        ...
      >
        <el-table-column
          v-for="option in tableOptions"
          :key="option.prop"
          :prop="option.prop"
          ...
        >
          <template slot-scope="scope">
            <el-form-item :prop="scope.row[option.prop].prop">
                  <el-input
                    v-model="formData[scope.row[option.prop].prop]"
                  />
...
<script lang="ts">
...
const formRules: { [key: string]: FormRule[]} = {
  smName: [{ required: true, trigger: 'blur' }],
  clientId: [{ required: true, trigger: 'blur' }],
  ...
}
...
const formData = {
      smName: '',
      clientId: '',
...
}).then(() => {
        (smKeyInfoForm.value as unknown as ElForm).validate(async (valid) => {
          if (valid) {
...

SomethingTab.vue

Upon testing, I discovered an issue where the form validation returns 'valid' even when a required field (such as smName) is empty.

After inspecting with Chrome DevTools Vue extensions, it appears that all el-form-item props are properly registered and v-model is correctly linked to the respective field. How can this validation problem be resolved?

Answer №1

The issue arises when the table element is switched from a plain div to a form-item and vice versa.

...
<div v-else-if="scope.row[option.prop].type === 'radios'">
                  <el-radio-group v-model="formData[scope.row[option.prop].prop]">
                    <el-radio
                      v-for="radioOption in scope.row[option.prop].options"
                      :key="radioOption.value"
                      :label="radioOption.value"
                    >
                      {{ radioOption.label }}
                    </el-radio>
                  </el-radio-group>
                </div>
                <div v-else>
                  {{ scope.row[option.prop] }}
                </div>
              </div>
            </el-form-item>

In display mode, the form shows 'divs', while it displays other form elements like radios in edit mode. It appears that form validation is not functioning correctly when transitioning into form-element mode.

To resolve this, I introduced a key to the form and adjusted it whenever toggling between show and edit modes, which successfully solved the issue.

...
 <el-form
      ...
      :key="formReRenderKey"
      :model="formData"
      :rules="formRules">
...
    const onClickBtn = () => {
        ...
        isOnModification.value = true

        formReRenderKey.value++
...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilizing property validation in your codebase

How can I set up my code to display a warning in the console if the value of the Prop is not a string? The player-name prop should always be a string. If this: <greet :player-name="5"></greet> contains a number, I want my code below to generat ...

Retrieve data from AJAX request using array index

A certain script in my possession is able to extract data from a JSON file and store the name property of each element in an array. HTML <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script> <script> var names = []; var f ...

What is the process for gaining access to additional plugins within a given plugin?

When working in a component's <script setup> block, I have the ability to access plugins using inject(). However, I am curious about how to gain access to other plugins' provide() method within a plugin that is used later on with use(). I ...

Challenges with loading content on the initial page load using the HTML5

Upon page load, I wanted to save the initial page information so that I could access it when navigating back from subsequent pages. (Initial Page -> Page2 -> Initial Page) After some trial and error, I ended up storing a global variable named first ...

What are some common applications of JSON?

Can you explain the purpose of JSON in Javascript and PHP and when it is necessary to use a JSON method? I checked out the link below but couldn't find any information on how JSON is implemented in actual projects. http://www.json.org/js.html ...

Import ES6 code by wrapping it in parentheses

Currently delving into React Native and I'm intrigued by the parentheses used in the first line of import. import React, { Component } from 'react'; import { AppRegistry, Text } from 'react-native'; class HelloWorldApp extends Co ...

Starting from TypeScript version 5.6, `Buffer` cannot be categorized as either `ArrayBufferView` or `Uint8Array | DataView`

Struggling to make the transition to TypeScript 5.6 Beta, I am encountering these error messages within the node_modules directory. Without making any other modifications, how can I resolve these errors? node_modules/@types/node/buffer.d.ts:632:19 - error ...

A universal TypeScript type for functions that return other functions, where the ReturnType is based on the returned function's ReturnType

Greetings to all TypeScript-3-Gurus out there! I am in need of assistance in defining a generic type GuruMagic<T> that functions as follows: T represents a function that returns another function, such as this example: fetchUser(id: Id) => (disp ...

Tips for Sending Variables in HTTP Requests in Angular 9

'''Is there a way to pass fromDateTime and toDateTime as parameters in this link?''' export function MSALInterceptorConfigFactory(): MsalInterceptorConfiguration { const protectedResourceMap = new Map<string, Array& ...

When using Vuejs + Laravel to make an Ajax request, only the most recent information entered into the view is submitted

Can someone guide me on implementing a basic ajax request using Vue with my Laravel backend? I have a boolean field called completed in a table named courses. In the view, users can see all assigned courses and toggle their completion status by pressing a ...

Add three rows without clicking, then click once to add one row at a time

Seeking guidance on how to defaultly display 3 rows after adding and removing rows, as well as applying the removal of a default set of 3 rows using JavaScript. Any valuable ideas are appreciated! Example needed:- https://i.sstatic.net/DF8Wn.png $(docum ...

Can someone explain the significance of this error message in Redux - Toolkit?

Encountered this error message while working with Redux-Toolkit, could someone explain its meaning? name:"ConditionError" message:"Aborted due to condition callback returning false." ...

Utilize PHP in an APP Engine Application to send an email to a Gmail address

I have a project deployed on Google App Engine where I need to send an email once a user submits the contact form. My app is successfully deployed and I have implemented an ajax request to a PHP file, but unfortunately, it's not functioning as expect ...

Is it possible to apply a class without using ng-class?

Seeking a more efficient way to apply conditional classes to multiple elements on a validation form page, I am exploring options to assign one of five potential classes to each form input. While the traditional method involves manually specifying the class ...

Updating the state of a Vuex store does not automatically trigger an update to a computed property in Vue3

I am currently working on an ecommerce website built with Vue.js 3 and Django, but I am facing some issues with the cart functionality. My goal is to update the number of items in the cart without reloading the entire page, just by updating the cart compon ...

Troubleshooting Issues with Passing Values in jQuery AJAX POST Requests

Currently, I am working on two basic PHP pages: notification.php <html> <head><title></title> <meta charset="UTF-8"> <script src="https://cdn.firebase.com/js/client/2.4.2/firebase.js"></script> <script src="ht ...

Changing the hidden input value to the data-id of a selected <a> element

I've set up a form with a dropdown menu and subdropdowns, generated using a foreach loop through a @Model. When I click on one of the options, I want the value of my hidden input field to match the data-id of the clicked item. This is what I have in ...

The access-control-allow-origin header is failing to receive updates

I need help figuring out how to manually set the Access-origin header. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge& ...

Exploring the concept of global objects within the expressjs framework

Currently, I am working on building a school management system. One issue I encountered is related to the creation of global objects in my backend when a teacher sends a post request. I am wondering whether multiple teachers accessing the server will res ...

What is the best way to update a table component on the screen in Vue after making changes to the JSON data?

Currently, I am at a beginner level and learning Vue. My main focus right now is to have the table in my application reload or re-render itself so that any changes made to the db.json file are reflected on the screen. I've implemented a modal that all ...