What is the best way to create a condition in a form from a dynamic scheme?
I am using a json list to setup my form, within my json list I wanna add a function where I can validate some logic to check if the field should be visible/required or not, or for example to validate the value with other value from the form.
This is a part of my json form:
..
"colour": {
compType: 'radiobox',
label: 'Colour',
options: [
{ value: 'red', label: 'Red' },
{ value: 'green', label: 'Green' },
{ value: 'blue', label: 'Blue' },
],
},
"model" : {
condition: testCondition, <----(Function which returns true if the field colour is red)
compType: 'select',
label: 'Role model',
subLabel: 'Who do you look up to?',
options: [
{ value: '', label: 'Select one', disabled: true },
{ value: 'captain-america', label: 'Steve Rogers/Captain America' },
{ value: 'iron-man', label: 'Tony Stark/Iron Man' },
{ value: 'thor-odinson', label: 'Thor Odinson' },
{ value: 'the-hulk', label: 'Bruce Banner/The Hulk' },
{ value: 'black-widow', label: 'Natasha Romanoff/Black Widow' },
{ value: 'hawkeye', label: 'Clint Barton/Hawkeye' },
],
},
..
Then I walk through it via my template.
...
<q-select v-else-if="prop.compType === 'select' && prop.condition"
outlined
v-model="dataRef[key]"
:options="prop.options"
:label="prop.label"
>
</q-select>
...
And the function should look something like this:
const testCondition = () => {
//how can I reach my ref Form data to check if the field needs to be visible
}
But because the form is not initialized yet I get an "Cannot access before initialization" error. Can someone help me on the right track? Will be more than enough for me.
PS: I know there are libraries that do this, but I'd rather I learn and understand it myself.