Vue-form-generator: Uncaught TypeError: Cannot read property 'validateAfterLoad' of undefined

Created on 1 Nov 2016  路  7Comments  路  Source: vue-generators/vue-form-generator

I get this error when I try to use the VueFormGenerator component without passing in an options property. For example,

<template>
    <vue-form-generator :schema="schema" :model="model"></vue-form-generator>
</template>

<script>
import { component as VueFormGenerator } from 'vue-form-generator';

export default {
    components: {
        VueFormGenerator,
    },

    data() {
        return {
            schema: {
                fields: [
                    { model: 'foo', type: 'number' },
                ],
            },
            model: {
                foo: 3.14,
            },
        },
    },
};
</script>

The bug is in the watch method in src/formGenerator.vue, where the library does not check if this.options is truthy before checking if this.options.validateAfterLoad is true. Reference.

There are two potential solutions to this problem: either set default values for the options property or add an additional check for this.options before access this.options.validateAfterLoad.


To set a default value, the library could define its props as:

<script>
export default {
    props: {
        schema: Object,
        options: {
            type: Object,
            default: {
                validateAfterLoad: false,
            },
        },
        // etc
    },
};
</script>

To just add the extra check, the code would need to change from:

// Model changed!
if (this.options.validateAfterLoad === true && this.isNewModel !== true)
    this.validate();
else
    this.clearValidationErrors();

to:

// Model changed!
if (this.options && this.options.validateAfterLoad === true && this.isNewModel !== true)
    this.validate();
else
    this.clearValidationErrors();

so that it matches the line later in the same file: Reference:

// First load, running validation if neccessary
if (this.options && this.options.validateAfterLoad === true && this.isNewModel !== true)
    this.validate();
else
    this.clearValidationErrors();
bug help wanted easy

All 7 comments

Thank you @player1537. Could you make a PR with this fix? I prefer the prop's default value solution.

Sure, I will make a PR when I get home later today. I'm having some trouble downloading one the packages in package.json (the kazupon/git-commit-message-convention one), but I think it will work from home.

OK, I will check this kazupon module, maybe it is an unused module

Fixed. Will be released in v0.6.0

Thank you! Sorry I didn't get to it in time.

No problem :)

Released

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gkurdej picture gkurdej  路  5Comments

pimhooghiemstra picture pimhooghiemstra  路  5Comments

blackfyre picture blackfyre  路  4Comments

reardestani picture reardestani  路  5Comments

sjordan1975 picture sjordan1975  路  5Comments