The rules function is expected to return a string or boolean value, but it received an 'object' instead

During my Vuetify validation process, I encountered an interesting situation: when I used data: () => ({, no errors were generated. However, upon changing it to data () {return {, an error surfaced stating: "Rules should return a string or boolean, received 'object' instead."

The code that triggered the error only appears when the entire page is reloaded and becomes no longer valid.

:rules="regla_buscar":

<template>
  <v-app>
    <v-form
      ref="form"
      v-model="form_valid"
      lazy-validation
      >
    <v-toolbar max-height="64">
      
      <v-btn icon color="indigo" to="/es/" nuxt>
        <v-icon>fas fa-filter</v-icon>
      </v-btn>
      <v-toolbar-title class="hidden-md-and-down">Red Social</v-toolbar-title>
      <v-spacer></v-spacer>
      <v-combobox
        v-model="model_categoria"
        :items="items_categoria"
        :search-input.sync="search"
        item-value="Clave"
        item-text="Nombres"
        hide-selected
        hide-details
        label="Categoria"
        persistent-hint
        small-chips
        class="mx-4 hidden-sm-and-down"
        @change="evento_categoria"
        >
      </v-combobox>
      <v-combobox
        v-model="model_pais"
        :items="items_pais"
        :search-input.sync="search"
        item-value="Clave"
        item-text="Nombres"
        hide-selected
        hide-details
        label="Pais"
        persistent-hint
        small-chips
        class="mx-4 hidden-md-and-down"
        @change="evento_pais"
        >
      </v-combobox>
      <v-text-field
        flat
        hide-details
        label="Buscar"
        prepend-inner-icon="mdi-magnify"
        single-line
        class="mx-4"
        clearable
        v-model="model_busqueda"
        v-on:keyup.enter="evento_busqueda"
        :rules="regla_buscar"
        >
      </v-text-field>
      <v-btn color="#1ebea5" dark
        to="/es/publish" nuxt
        >
        <v-icon>fas fa-upload</v-icon><div class="mx-4 hidden-sm-and-down">Publicar</div>
      </v-btn>
            
    </v-toolbar>
    </v-form>
      <v-main>
        <nuxt keep-alive />
      </v-main> 
    </v-app>
</template>

<script>
  import categorias from '../archivos/Categorias';
  import paises from '../archivos/Paises';
  import sexos from '../archivos/Sexos';
  import relevancia from '../archivos/Relevancia';

  const clase="flag-icon flag-icon-";
  export default {

    data() {                                   // <== changed this line
      return {
        form_valid: true,
        drawer: null,
        model: ['Vuetify'],
        search: null,
        icons: [
          'fab fa-whatsapp',
          'fab fa-facebook-f',
          'fab fa-telegram-plane',
          'fab fa-slack',
          'fab fa-discord'
        ],
        model_categoria: '',
        model_pais: '',
        model_relevancia: '',
        model_busqueda:'',
        items_categoria: [],
        items_pais: [],
        items_relevancia: [],
        select: { state: 'Español', abbr: 'es' },
          items: [
            { state: 'Español', abbr: 'es' },
            { state: 'Ingles', abbr: 'en' },
            { state: 'Portuges', abbr: 'pr' },
            { state: 'Aleman', abbr: 'al' },
            { state: 'Frances', abbr: 'fr' },
          ],
        disabled: false,
        absolute: false,
        openOnHover: false,
        value: false,
        closeOnClick: true,
        closeOnContentClick: true,
        offsetX: false,
        offsetY: true,
        busqueda:'',
        regla_buscar: [
          v => (!v || (v && v.length > 2 && v.length <= 30)) || 'Ingresa minino 3 carateres',
        ]
      }
    },
    async fetch() {
      this.items_categoria = categorias;
      this.items_pais = paises;
      this.items_relevancia = relevancia;
    },
    methods: {
      evento_busqueda () {
        if(this.$refs.form.validate()){
        this.busqueda = this.model_busqueda;
        let parametros = this.evento_parametros();
        this.$router.push({path: '/es/', query : {
          xca:parametros.xca,
          xpa:parametros.xpa,
          xre:parametros.xre,
          xbu:parametros.xbu,
          }})
        }
      },
      evento_categoria () {
        let parametros = this.evento_parametros();
        this.$router.push({path: '/es/', query : {
          xca:parametros.xca,
          xpa:parametros.xpa,
          xre:parametros.xre,
          xbu:parametros.xbu,
          }})
      },
      evento_pais () {
        let parametros = this.evento_parametros();
        this.$router.push({path: '/es/', query : {
          xca:parametros.xca,
          xpa:parametros.xpa,
          xre:parametros.xre,
          xbu:parametros.xbu,
          }})
      },
      evento_relevancia () {
        let parametros = this.evento_parametros();
        this.$router.push({path: '/es/', query : {
          xca:parametros.xca,
          xpa:parametros.xpa,
          xre:parametros.xre,
          xbu:parametros.xbu,
          }})
      },
      evento_parametros(){
        let categoria = undefined;
        if (this.model_categoria != null){
          categoria = this.model_categoria.Clave;
        }
        let pais = undefined;
        if(this.model_pais != null){
          pais = this.model_pais.Clave;
        }
        let relevancia = undefined;
        if(this.model_relevancia != null){
          relevancia = this.model_relevancia.Clave;
        }
        if(this.busqueda == ''){
          this.busqueda=undefined
        }
        let parametros = {
          xca: categoria,
          xpa: pais,
          xre: relevancia,
          xbu: this.busqueda
        }
        return parametros;
      }
    }
  }
</script>

Answer №1

Encountered the same problem myself.

After some investigation, I discovered that the presence of the nuxt fetch() hook during server-side rendering causes the v-text-field rules logic to trigger an error consistently (even with an empty fetch method).

Switching from fetch() to created() or mounted() resolved the issue for me.

Answer №2

It could be possible that the issue lies here!

When setting rules for the v-text-field, ensure that "regla_buscar" is passed as an array, not an object

The error may be due to an extra comma at the end of your regla_buscar array

  regla_buscar: [
              v => (!v || (v && v.length > 2 && v.length <= 30))  || 
'Please enter at least 3 characters', <===== this extra comma
            ]

Certain browsers may interpret ["your Result", ] as ["your Result", null] where null is considered an object

Additionally, you can switch your mode to "spa" and test it out

Answer №3

When utilizing fetch(), it is advisable to eliminate the

regla_buscar: [v => (!v || (v && v.length > 2 && v.length <= 30)) || 'Ingresa minino 3 carateres']
from the data property

Instead, incorporate it as a computed property in the following manner

regla_buscar: function() {
  return [v => (!v || (v && v.length > 2 && v.length <= 30)) || 'Ingresa minino 3 carateres']
}

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

How can methods access variables from the Vuex store during function calls?

Within my Vue.js component, there is a v-select element. Upon user selection in this widget, the toDo function is triggered from the methods block. However, when attempting to access the value of the filters getter within this function, it consistently ret ...

Employ React Hooks for modifying the class names of elements and their related siblings

I have a function component in React hooks that I am working on. The goal is to change the className to 'active' when any element in the list is clicked, and remove the className from the other elements. const SideBar = () =>{ const [active ...

Upon attempting to open Google Maps for the second time, an error message pops up indicating that the Google Maps JavaScript API has been included multiple times on this page

Currently, I am utilizing the npm package known as google-maps and integrating it with an angular material modal to display a map. However, upon opening the map for the second time, an error message is triggered: You have included the Google Maps JavaScri ...

Getting the environment variable from Rails in your JavaScript: A guide

I am currently working on implementing a key system within my application. In the code I am using, there is logic that looks like this: $('.key-test-button').on('click', function(){ if ($('.key-test-input').val() === "MYK ...

Utilizing a GLTF asset as the main Scene element in a Three.js project

I'm struggling with incorporating a gltf model as the main scene in Three.js. Specifically, I have a gltf model of an apartment that I want to load from inside and not outside the apartment. I also need the controls to work seamlessly within the apart ...

Passing arguments to an external function in jQuery from a dynamically loaded Ajax page

Despite its confusing title, the issue at hand is actually quite simple. My homepage contains a script that loads an external PHP file for a specific section of my website. Within this PHP file, I need to call a function from the main JavaScript file (th ...

What is the process for logging in using a Bootstrap modal?

I am currently working on creating a login feature using a bootstrap modal, but unfortunately, I am facing some issues with it. If anyone is willing to offer their assistance, I would greatly appreciate it. My knowledge of JavaScript and Bootstrap is limi ...

The usage of $('').switchClass in IE8 may cause an error when the switched class includes a color property

I have the following unique css classes .swap-format{ background-color: green; } .swap-format1{ background-color: orange; } .swap-format2{ color: purple; } Using these classes, I want to create an animation on the given div <div id="swap-clas ...

Forcibly altering the title name in HTML: a step-by-step guide

Currently, I am working on a project that consists of over 30 JS files and an index file. I recently changed the title of the page in the index file, but the new title only displays when I refresh the page. Once the refresh is complete, the old title reapp ...

Retrieve information from Node.js using Express

I am working on a server with Node.js and using Express to create a web application. I have successfully implemented a function called rss_interrogDB that retrieves an array from a database. Now, I am trying to use this array to display a list on the HTML ...

Unable to reset HighCharts Speedometer value to zero

I am currently experimenting with implementing the highcharts speedometer. The goal is to simulate a connection meter - when there is a good connection, the meter should display values between 30 and 100. If the connection result from an Ajax call is any ...

Creating a new array in Vue.js by filtering the results of a promise iteration

Is there a way to use the splice method to insert promise values from an old array into a new one for vue reactivity? I'm encountering an issue where the newArray remains empty and does not receive any values. Check out this link for more information. ...

Retrieve the file path of the local system's home directory from the server

The server is up and running, I am looking to retrieve the file path of the local system in which the AWS server is operating using Node.js. ...

Can you use the OR operator between vee-validate3 rules?

For this input, the value can be either a username or email address. Here is an example: <ValidationProvider name="email" rules="username || email" v-slot="{ errors, valid }"> <v-text-field v-model="something" :error-messages="er ...

The form is not being submitted when I click on the submit button because I have two buttons that trigger AJAX requests

I am facing an issue with a form where there is a submit button inside it, as well as another button within the form that has attributes type=button and onclick=somefunction(). The button with the onclick function works perfectly fine, but the other button ...

Is there a way to upload a video, file, and PDF to a database using multer?

There is a database schema defined in the code below: const mongoose = require("mongoose"); const FormCourse_schema = new mongoose.Schema({ FormCourse: { type: mongoose.Schema.Types.ObjectId, ref: "cards", required: true, ...

Jest does not recognize AnimationEvent as a defined element

I am currently facing an issue while attempting to simulate an animationEvent for a test within my Angular application. The error message I receive is: ReferenceError: AnimationEvent is not defined. Given that this feature is experimental, it seems like ...

Vue.js does not apply the "selected" attribute to set the HTML <option> tag to default

Trying to designate a default <option> tag from the available options. Utilizing v-model on the <select> tag, the vue.js documentation indicates the following approach: v-model will disregard the ... selected attributes present on form ele ...

Embedding Google+ Sharing in an iframe

I'm currently working on a web application that needs to be compatible with PC, tablets, and mobile phones. I'm interested in integrating Google+ Sharing into the app. However, it appears that when using the url , there are issues with blocking ...

Retrieve recently appended DOM elements following the invocation of createComponent on a ViewContainerRef

I have a current function in my code that dynamically creates components and then generates a table of contents once the components are added to the DOM. This service retrieves all h3 elements from the DOM to include in the table of contents: generateDy ...