Vue-form-generator: Conditional fields

Created on 11 Aug 2016  路  6Comments  路  Source: vue-generators/vue-form-generator

It would be great to have conditional fields. The value of one field influence others visibility (and maybe more ?).
For example, I check a checkbox field, and it show another field.
It could allow for very dynamic forms.
The field would need some sort of meta information about them, for example:

/*schema.js*/
fields: [{
    type: "switch",
    label: "Add a packaging ?",
    model: "packet"
}, {
    type: "select",
    label: "packet type",
    model: "packet_type",
    required: true,
    values: [
            { id: "standard", name: "Standard" },
            { id: "present", name: "Present" }
        ]
    // Only appear if packet value is true
    dependOn: [{ model: "packet", value: true }]
}, {
    type: "text",
    label: "message to write on the note",
    model: "present_note",
    // Only appear if packet_type selected value of "id" field is "present" AND if packet value is true
    dependOn: [{ model: "packet_type", field: "id", value: "present" }, { model: "packet", value: true }]
}]

They could be option for "not this value" and "multiple good values".
Internally, it could use v-show on a computed value that we could add in abstractField.js.
That's just some first ideas and thoughts, please tell me what are you think about that.

Most helpful comment

This is an exist function. Check the company.name field in dev example. You can set a visible function what lib will execute.

{
            type: "text",
            label: "Company name",
            model: "company.name",
            styleClasses: ["company", "half-width"],

            visible(model) {
                return model && model.type == "business";
            }
        }

So this fields is visible if the model.type is "business".

_I see it is missing from docs_

All 6 comments

This is an exist function. Check the company.name field in dev example. You can set a visible function what lib will execute.

{
            type: "text",
            label: "Company name",
            model: "company.name",
            styleClasses: ["company", "half-width"],

            visible(model) {
                return model && model.type == "business";
            }
        }

So this fields is visible if the model.type is "business".

_I see it is missing from docs_

And the disabled properties can be a function too :) Similar as visible

Great ! Please add it to the documentation, this is a very useful functionality.

Wouldn't it be nice to have a declarative way to indicate the dependency, like all the rest of the schema is, instead of functional?

It would be nice to be able to tell VFG if disabled/hidden fields should be validated too. Like, sometimes you have a hidden/disabled conditional field that is required only when it's visible/enabled.

https://github.com/vue-generators/vue-form-generator/issues/35#issuecomment-467933897
I mean, that snippet in the example above

visible(model) {
    return model && model.type == "business";
}

...isn't valid JSON, right?
Whereas something like JSONLogic would be valid JSON. A means, using JSON, to indicate "this field depends on that field's value x".

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pimhooghiemstra picture pimhooghiemstra  路  5Comments

Atiladanvi picture Atiladanvi  路  4Comments

kiankji picture kiankji  路  4Comments

LouWii picture LouWii  路  4Comments

DelfsEngineering picture DelfsEngineering  路  4Comments