agriboz
agriboz

Reputation: 4854

Vuelidate empty object on select validation issue

I would like to know how to validate empty object using vuelidate. I tried to give a demonstration on jsfiddle as links follows

Vue.use(window.vuelidate.default)
const { required, minLength } = window.validators

new Vue(
    {
        el: "#app",
        data: {
            companies: [
                {
                    id: 1,
                    name: 'facebook'
                }, 
                {
                    id: 2, 
                    name: 'apple'
                }
            ],
            text: {
                id: null,
                name: null
            }
        },
        validations: {
            text: {
                required
            }
        }
    }
)

jsfiddle

Upvotes: 0

Views: 10971

Answers (3)

agriboz
agriboz

Reputation: 4854

There is missing information about how to use withParams in the documentation of vuelidate page. So i have searched on its github page and found this link . According to link i came up with that solution

import { withParams } from 'vuelidate'

export const checkIfNull = withParams(
  { type: 'required' },
  value => (value.id === null ? false : true)
)

Upvotes: 1

oniondomes
oniondomes

Reputation: 2063

$v.text is valid because it is a non-empty object. That means it doesn't have 'falsy' value so it meets the requirement. One way to make it work:

validations: {
    text: {
        id: {required},
        name: {required},
    },
},

JSFiddle

If you don't want to repeat items object structure, you can write a custom validator.

Upvotes: 4

Phunky
Phunky

Reputation: 481

There is nothing special about validating an object, you just need to define the structure and add any validation rules you require.

Please see the example I created and take another look at the Collections docs.

Upvotes: 0

Related Questions