Checking for valid single values in an array

I am handling an array in my project where values are either true or false. For this task, I am utilizing vuelidate.

selectedDays: [
        false,
        false,
        false,
        false,
        false,
        false,
        false
    ],

In my template, the checkboxes toggle the values between true and false. A validation rule needs to be implemented to ensure that at least one checkbox is checked. If none are selected, the validation should trigger.

<p class="invalid-feedback mt-3" v-if="v$.selectedDays.$dirty && v$.selectedDays.$error">Please select workout days</p>
<template v-for="(item, index) in daysList" :key="item.id">
<v-checkbox
 hide-details
 color="indigo"
 v-model="selectedDays[index]"
 :label="item.title"
 >
 </v-checkbox>
                                
 </template>

This is the initial validation approach:

        selectedDays: {
            $each: helpers.forEach({
                sameAs: sameAs(true)
            })
        }

Unfortunately, this method does not achieve the desired outcome. I attempted to implement a custom validation logic:

let check = this.selectedDays.some(function() {
if (this.selectedDays === true) {
    return true;
} else {
    return false;
}});

However, I am unsure how to integrate this custom validation within vuelidate. Any assistance would be greatly appreciated. Thank you!

Answer №1

The validation process needs to be updated to check if any element in the array is true:

let isValid = this.selectedDays.some(function(day) {
if (day === true) {
    return true;
} else {
    return false;
}});

Alternatively, you can use a more concise arrow function:

let isValid = this.selectedDays.some(e => e === true);

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

Learn to leverage JavaScript in Node-RED to dynamically display messages based on selected dropdown options

After reviewing this Node-red flow: https://i.stack.imgur.com/y4aDM.png I am struggling with the implementation in my function. There are 3 options in each dropdown, one for meat type and the other for doneness. Once I select a combination from the drop ...

Blending two materials in THREE.js on a single mesh with depth control

I'm looking to apply two different materials to a single mesh named "mesh1." mat1 will be red in color, while mat2 will be blue. The distance between the camera and the mesh is set at "1 unit." When mesh1 is close to the camera (between 0.1 and 0.4 ...

The key to subscribing only once to an element from AsyncSubject in the consumer pattern

When working with rxjs5, I encountered a situation where I needed to subscribe to an AsyncSubject multiple times, but only one subscriber should be able to receive the next() event. Any additional subscribers (if still active) should automatically receive ...

transfer jquery element using jquery ajax

How can I send a jQuery object to a PHP function using POST? When I stringify the object, I get the following output: [ { "id": "701", "user_id": "2", "playlist": "ukg%20garage", "tracks": "5", "thumbnail": "Coldplay.jpeg", "crea ...

How can I prevent ajax loader from overlapping headers in jquery-datatables?

Currently, I am developing a web application that utilizes jQuery data tables to load data from the server side. One issue I have encountered is that when the data table first loads, the loader covers the headers, creating a visibility problem as depicted ...

What are the steps to enable a Vue component to handle its own transitions?

I am looking for a way to handle enter and leave animations in Vue when components are mounted or removed. My goal is to consolidate all the animation code into its own component for better organization. <template> <transition @enter="enter" ...

Tips for setting up a material-ui theme on the go

How can the material-ui theme object be dynamically configured? This feature is activated by clicking on the "set docs colors" button located on the documentation page of mui. ...

Enhancing ajax requests with headers in Ember.js

My goal is to configure request headers for my emberjs application. However, when setting it up in the initializer, the client_id appears as [object Object] instead of the actual value. This is the initializer that runs when the application starts. Apikey ...

Troubleshooting port deployment challenges on GCP for a Node.js project

My current Node.js project has one HTTP server running on port 8080, with a service URL generated for it. However, inside the http.createServer function, I have set up another server for an NLP engine that is listening on port 8081. While I am able to ac ...

Invoke the function functionA within the ClassComponentA class in the file fileA, located within the functionComponentA file of a REACT 17 application

My dilemma involves working with two components: a class component and a function component. I am trying to find a way to call a method from functionComponentA, which is located in classComponentA. Currently, I am achieving this by passing the method as pr ...

Steps to develop a JavaScript countdown timer that resumes counting even after refreshing the page

I've been trying to figure it out with JavaScript and jQuery, but I'm stumped. I want the timer to not reset when the page is refreshed. It's like an auction countdown where it goes from 3,2 and if you refresh, it should pick up where it lef ...

Securing your Vue component with CSRF token

I have a Laravel 5.3 project that is integrated with Vue.js and I need to utilize the CSRF-TOKEN in my form. The form html code is located in a Vue component file within resources / assets / js / bootstrap.js Currently, I have the following: Vue.http.in ...

When submitting a form with a contenteditable div and text box using JQuery, the POST request is not

I developed a website that allows users to edit and format text, then save it onto the server. Utilizing jquery to send the data to a PHP page for saving, I encountered an issue where the site fails to send the file name along with the formatted text to PH ...

Ways to boost memory capacity in nodejs

I've been attempting to increase the memory limit in node.js, and to do so I added an environment variable. You can see how I did it here Unfortunately, it doesn't seem to be working as expected. I even tried using capital letters with no succe ...

What could be causing the issue with the JS function that is preventing the button from

I am generating a button dynamically using my JavaScript function and then adding it to the DOM. Below is the code snippet: var button = '<button id="btnStrView" type="button" onclick=' + parent.ExecuteCommand(item.cmdIndex) + ' c ...

When trying to pass context to the interactive node shell, an error message may appear stating "TypeError: sandbox argument must be converted to a context"

I am trying to initiate an interactive node shell with pre-initialized objects. But when I use the code below, it gives me an error: var repl = require('repl') var x = 11, y = 21 var con = {} con.x = x con.y = y repl.start('> &apo ...

Different ways to trigger events with the press of a single button

Is there a way to send an event to the backend where 'isVerified' can be true or false based on the last condition, with only one button form available? <form onSubmit={(ev) => this.submit(ev, 'isVerified')}> ...

"Implementing angularJS html5mode within a controller: A step-by-step guide

Recently, I have been trying to familiarize myself with AngularJS. I'm attempting to set the html5 mode to true in a specific controller within my AngularJS application which consists of three controllers. Despite my efforts to configure the setting u ...

A step-by-step guide on fetching multiple iframe contents simultaneously with Selenium in Java

Today, I am facing an interesting challenge. I need to extract the page source of a webpage that includes an iframe. Surprisingly, when using the PhantomJSDriver with the code snippet below, I am only able to retrieve either the page content or the iframe ...

Adjust the appearance of "FAQS" to show as "FAQs" in Lodash

I've integrated the lodash library - a powerful JavaScript utility tool - into my AngularJS project. <div ng-repeat="question in questions"> <label>{{question | startCase}}</label> </div> If you want to learn more about th ...