Using Vue to pass an array of rules to a child component

Currently, I am passing a set of props called "propSet" to a child component. These props are computed and detect an 'edit mode' boolean that changes accordingly.

The "propSet" defines the following form input props: color, filled, dense, outlined, readonly, required, and rules.

All the props work fine, except for "rules". Whenever a child component uses the 'editMode == true' configuration of "propSet," I receive the following error message in the console:

[Vue warn]: Invalid prop: type check failed for prop "rules". Expected Array, got Object

I have tried various solutions, including:

  • Adding type validation to the Child Component for the rules array

    • props: ['propSet'] //original
    • props: { propSet: Array } //updated
  • Using Object.entries() to convert the object to an array

    • var rulesSet = Object.entries(this.rules) 
  • Adjusting the configuration of rules

    •  //original  
       rules: {
          required: (v) => !!v || "This field is required",
          autoComplete: (v) => !!(v && v.length) || "This field is required",
        },
      
    •  //updated
       rules: [{
         required: (v) => !!v || "This field is required"},
         autoComplete: (v) => !!(v && v.length) || "This field is required",
        }],
      

Despite reading the documentation for Props multiple times and exploring related sources such as the Vuetify API, I haven't found a solution yet. I've also checked several Stack Overflow questions regarding Vue.js, props, and components without success.

I suspect there might be something obvious that I'm overlooking. For simplicity's sake, here is a codepen with a different set of props that showcases the issue:

Pass-Rules-As-Prop

Answer №1

Ensure that the rules prop of Vuetify's Text field component is structured as an array:

Vuetify provides a straightforward validation process through the rules prop. This prop requires an array that includes various types such as function, boolean, and string. Upon each change in the input value, every element within the array will undergo validation. Functions are designed to take the current v-model as an argument and should return either true / false or a string with an error message.

Visit: Text field component - Vuetify

However, it appears that within your parent's data structure, you have defined the rules as an object containing your validation functions instead of storing them within an array:

    rules: {
      required: (v) => !!v || "This field is required",
      autoComplete: (v) => !!(v && v.length) || "This field is required"
    }

To rectify this issue, modify the structure to match the following format:

    rules: [
      (v) => !!v || "This field is required",
      (v) => !!(v && v.length) || "This field is required"
    ]

Keep in mind that both versions involve declaring anonymous functions. If you prefer retaining the function names for reference purposes, consider utilizing named functions like this:

    rules: [
      function required(v) { return !!v || "This field is required"; },
      function autoComplete(v) { return !!(v && v.length) || "This field is required"; }
    ]

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

Run Nuxt.js and Express.js seamlessly on an Ubuntu server using Putty, ensuring continuous operation

After completing a project using Express.js and Nuxt.js, I attempted to build and run it on my Ubuntu server via Putty. However, the result was not as expected. https://i.stack.imgur.com/Zlua4.png I am now wondering if there is a way to make this project ...

Issue with data-ng-class function not being invoked

I'm currently working on a simple Angular project where I need to dynamically color list items based on a function called in data-ng-class. Below is an excerpt from my HTML file: <div> Rooms:<ul style="list-style:none;"> < ...

python using selenium to bypass javascript popups

I have a project where I need to visit a specific website and interact with it using the Python selenium module. However, I am encountering a popup (a cookie acceptance form) when trying to access the site with a bot. I must accept this popup in order to p ...

JQuery restricts input to only allow numbers with a set number of digits, ensuring uniqueness

Can someone assist me in creating a jQuery script that only allows unique numbers with exactly 4 digits in a numeric field? Here is the HTML code: <input type="text" id="registration_number" class="input-sm" name="registration_number" maxlength="6" re ...

What steps can I take to guarantee that a select change event occurs following the setting of ngmodel in Angular2?

I am facing an issue with a select element wherein a basic (change) handler is attached along with an ngmodel. Whenever an <option> with a ng value is set, the change handler triggers before the [(ngModel)] reflects the new values. <div> ...

Extracting text from HTML response and implementing a condition in Node.js

Hello, I am a beginner when it comes to node js and haven't had much experience with it before. I could really use some assistance in resolving an issue that I am facing. Below is the code snippet that I am working with: async function reserved_slot(p ...

Shaky parallax movement

I recently created a website with a parallax effect, but I'm experiencing some performance issues with jittery movement. The page uses CSS3 transform scale to zoom in and out, and automatically resizes on page resize with JavaScript/jQuery. For the ...

Create a VueJS/VuetifyJS implementation inspired by the WhatsApp swipe between tabs animation

Currently, I am utilizing the VuetifyJS framework for VueJS and I am interested in replicating the swipe between tabs transition seen in WhatsApp for Android. In WhatsApp, you have the ability to swipe left or right to view a new section as you swipe. Vue ...

The getList() function from Restangular is failing to return the expected JSON data

I'm facing an issue with retrieving data from a Restangular promise. Instead of receiving pure JSON data, I always end up with a promise object. Here is the response from my API: localhost:3000/api/meal { "status": "success", "data": [ { ...

Try utilizing a variety of background hues for uib progressbars

Looking to incorporate the ui-bootstrap progressbar into my template in two different colors, background included. My initial idea was to stack two progress bars on top of each other, but it ended up looking too obvious and messy, especially in the corner ...

Initiate the React application with the given external parameters

I have created a React app that is embedded within a webpage and needs to start with specific parameters obtained from the page. Currently, I am passing these parameters in the index.HTML file within a div element. The issue arises when these parameters ar ...

Understanding the concept of mutable properties in Typescript

Why can the property 'name' in the class 'PersonImpl' be reassigned even though it is not declared as read-only in the Person interface? interface Person { readonly name: string; } interface Greeting extends Person { greet( ...

Increase the gap between the legend and the chart when utilizing charts.js

I'm currently working on a project using charts.js and running into a slight issue. The legend for my bar chart is overlapping with the values displayed. I've been attempting to troubleshoot this problem without much success so far, so I would g ...

iOS Safari browser does not support changing the caret color in textarea

Seeking a solution to hide the text cursor (caret) from a textarea on iOS browsers like Safari and Chrome. Despite trying the caret-color property, it does not seem to work. Are there any alternative methods to achieve this? One approach I attempted is b ...

Manipulate the value(s) of a multi-select form field

How can you effectively manage multiple selections in a form field like the one below and manipulate the selected options? <select class="items" multiple="multiple" size="5"> <option value="apple">apple</option> <option va ...

Transferring information among various instances of a single controller (ng-controller)

I am relatively new to using Angular and I am facing a challenge with a seemingly simple task that is proving to be more complex within the framework. The issue at hand involves data manipulation, specifically with a variable named var1. I am modifying th ...

Guide to creating a Discord bot that replies privately to users without other channel members being able to see the messages

As a newcomer to Discord, I am attempting to create a bot that will respond to user commands by sending a reply that only the user who issued the command can see. However, I am struggling to figure out how to implement this feature. Below is the source c ...

Creating a double-layered donut chart with Chart.js

I'm attempting to create a unique pie chart that illustrates an array of countries on the first level and their respective cities on the second level. After modifying the data in a JSON file to align with my goal, it doesn't seem to be working a ...

A pale tooltip with a light arrow appearing against a soft white backdrop

Can someone help me figure out how to display a white tooltip with a white arrow? I've tried to implement it using the code below, but the white color is not visible against the background. Any suggestions on making it stand out? https://i.sstatic.ne ...

Testing Vue.js Components with Mocks and Unit Tests

When writing unit tests for a Vue.js 2 application that utilizes Vuex as a store, I have noticed a recurring pattern in many of my components: For example, consider the 'thing.vue' component: <template> <div> {{ thing.la ...