What is the best way to authenticate a child component with Vue Ant Design?

I am facing an issue with my DynamicSelect component (child component) when utilizing it in another component (parent). The problem arises when I attempt to validate the child component, as it consistently returns a null value, resulting in failed validation.

Below is the code snippet for the DynamicSelect Component:

<template>
  <a-select
    :showSearch="true"
    :placeholder=placeholder
    :value="selectedValue"
    @search="searchRegex($event)"
    @change="$emit('changed-item', setChangedItem($event))"
    @select="$emit('selected-item', setSelectedItem($event))"
    :filterOption="filterOption"
  >
    <a-select-option
      v-for="(item,idx) in dropdownData"
      :value="idx"
      :key="idx"
    >{{item.text}}</a-select-option>
  </a-select>
</template>

<script>
// JavaScript logic here
</script>

Here is how the parent component utilizes the DynamicSelect component:

<template>

<dynamic-select
                      :dataSrc="users"
                      placeholder="Lastname, Firstname"
                      @selected-item="onSelectUser($event)"
                      @changed-item="onSelectUser($event)"
                      :lookFor="['lastname','firstname']"
                      v-decorator="['contact', {valuePropName:'selectedValue', 
                      rules: [{ required: true, 
                                validator: userExists, 
                                 message: 'Error'}]}]"
              >
</dynamic-select>

</template>

<script>
// More script content
.
.
.
methods: {

userExists(rule, value, callback) {
        console.log('VALUE', value); //always undefined
        console.log('RULES', rule);
        console.log('CALLBACK', callback)
        return value !== null && value !== undefined && value.length > 2;
      },

onSelectUser(user) {
        console.log("user: " , user); // set with the selected value
       }
},
.
.
.
</script>

The expected behavior is for the child component to correctly return the selected value similar to emitting an event. I have also experimented with models but without success. Your assistance on this would be greatly appreciated. Thank you!

Answer №1

Inter-component communication made simple

Vue.config.debug = true;

// Main Component
let App = new Vue({
  el: "#the-parent",
  
  data(){
    return{ msg: "No message yet" };
  },
  
  methods:{
    receivedFromChild(request){
      this.msg = request;
    }
  },
  
  
  // Child Components
  components: {
    'children': {
      template: `
        <div><button @click="request">Send to parent!</button>` + `<input type="text" v-model="text"></div>`,
      
      props: [ 'childrenRequest' ],
      data() {
        return {
          text: 'this is value'
        }
      },
      methods: {
        request(){
          console.log('working!');
          this.$emit('received', this.text);
        }
      }
    
    }
  }

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="the-parent">
    <h3>The children want me to say: {{ msg }}</h3>
    <children @received="receivedFromChild"></children>
</div>

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

Error encountered while compiling React application: Module build unsuccessful due to failure in ./node_modules/babel-loader/lib/index.js

Having an issue while trying to compile a React app. After pulling the repo from Github, running yarn install, and then compiling it, I encountered the following error: Module build failed (from ./node_modules/babel-loader/lib/index.js) SyntaxError: {file_ ...

Django: The style from ... was rejected for application because its MIME type of 'text/html' is not a supported stylesheet MIME type

Struggling to link my style.css file in a Django template using static files {% static 'assets/css/style.css' %}. However, I keep encountering the error message Refused to apply style from 'http://127.0.0.1:8000/assets/css/style.css' be ...

Discovering the correct location within a complex JSON or array to perform updates using JavaScript (either AngularJS or vanilla JavaScript

I am currently facing a challenge where I need to search for a specific value within my complex JSON array and then update the corresponding object. Here is a snippet of my JSON array: var myArray = [{ "id": 5424, "description": "x ...

Create a polling feature using a Grease Monkey script

I am looking for a way to run a Tamper Monkey script on a Facebook page that regularly checks a database for new data and performs certain actions. I have attempted to implement polling using AJAX, and below is the code I used: (function poll() { setT ...

Most effective method for utilizing Ajax to upload files and ensuring they are uploaded before submission

I am currently working on a form that contains various fields, including file upload functionality for multiple files. I have also explored the option of using AJAX to upload files. My goal is to seamlessly upload files via AJAX while simultaneously fillin ...

In my Vue watch method, I have two parameters specified, and one of them remains constant without any changes

Currently, I am attempting to access a method within my watch function with two parameters. Here is the code snippet for the method: onBoolianChange(value, willChange) { willChange = (value === false) ? true : false; }, watch: { "e ...

Issue encountered in TypeScript: Property 'counter' is not found in the specified type '{}'.ts

Hey there, I'm currently facing an issue while trying to convert a working JavaScript example to TypeScript (tsx). The error message I keep encountering is: Property 'counter' does not exist on type '{}'.ts at several locations wh ...

Ways to integrate a spinner during the processing of data

I am looking for a way to display a spinner while data is being processed in my 'Upload' method. Currently, I am using EPPlus 'IFormfile' to read the data from the input and pass it to the upload method. However, I am wondering if there ...

a guide on retrieving FormData objects in PHP

Hey everyone, I have a question regarding a sample code snippet I posted here. In this code, I am successfully uploading a file using Ajax JQuery. However, I am struggling to figure out how to read the content of the uploaded file in my PHP code. Can anyon ...

Regular expressions can be used to extract all attributes from a webpage that begin with a specified value

I have a simple question - how can I retrieve the values of all attributes that start with http://example.com/api/v3?? For instance, if a webpage includes: <iframe src="http://example.com/api/v3?download=example%2Forg"> <meta twitter="http://exam ...

Steps to save a checklist on your device

Hey, I recently created a to-do list using Vue.js. You can check it out here. I'm facing an issue where I am unable to save (download) the list to my device using the Save option. Can anyone provide some assistance with this problem? Your help would b ...

Exploring the power of Next.js dynamic routes connected to a Firestore collection

Currently seeking a solution to create a dynamic route that will display each document in a Firestore collection using Server-side Rendering. For instance, if there is a document named foo, it would be accessible at test.com/foo under the [doc] page compo ...

Mocking a Promise-returning dependency for a React Component in Jest

There's a React component I'm working on that has a dependency like so: import { fetchUsers } from '../../api/'; This function is a utility that returns a Promise. My challenge lies in trying to mock this dependency using Jest. I&apo ...

Preventing CSS blocking with Node on Heroku by using X-Content-Type-Options

I am facing a challenge deploying my Node.js application to Heroku. The frontend of the app is built using Vue.js and the Vue build output is located within the /public directory of the Node app. While everything functions perfectly when accessed from loca ...

What is the primary function for Express.js and Node.js 10 in Google Cloud Functions?

My Node JS version is 10 Express JS version: 4.16.1 I understand that for Node JS 8, the entry point should be "app" The generated code (from `npm install -g express-generator) www #!/usr/bin/env node /** * Module dependencies. */ var app = require( ...

Customizing a carousel in Next JS to loop through items and control their visibility

I've created a custom Next JS carousel that tracks the current slide index and displays an image or video accordingly. However, it appears that because the carousel is set to autoplay and each slide is removed and readded every 6 seconds, the page dow ...

What is the best way to extract the internal ID or class from a div element

I have a situation similar to this: <div id="container"></div> This is the HTML code. I am dynamically adding additional div elements inside the container using jQuery. $(document).ready(function () { $.get("/Ask", {}, function (response ...

Clicking the jAuery selection button on the website allows

I have a table with buttons that I need to customize for selection, using CSS. The goal is to style the buttons differently than standard checkboxes or radio buttons by using the type "button" and applying CSS/CSS3 styles. <div class="button"> ...

Is it possible for PHP to use the set cookie function to replace the cookie value set by JQuery cookie?

I'm facing an issue where I want a single cookie to be set and its value updated by PHP when a user logs in. However, currently it seems to just create a new separate cookie each time. Below is the code snippet where I am trying to set the cookie valu ...

How to access and retrieve selected checkbox values using jQuery

<form id="myform"> <input type='checkbox' name='foo[]' value='1'> <input type='checkbox' name='foo[]' checked='true' value='2' > <input type='checkbox' ...