0

I am building an application which will generate a dynamic form for customer feedback reception. Now while adding fields to the form, individual fields have validation rules inlcuding length and type rules, there is also going to be a property on each validation rule, which will determine if that particular rule should be parsed while validating the fields.

I have a Vue data property called fields, which has an object for each field. It looks like this:

export default {

    data() {
        return {
            fields: [{
                label: 'Email',
                validation: {
                    length: {
                        add: 0, // 0 for false, 1 for true
                        min: 0, // minimum length
                        max: 0 // maximum length
                    }
                }
            }]
        }
    }

}

Now for enabling the user to select whether validation rule should be enabled or disabled (i.e length.add = 1 or 0) , I have a a select element with two options for their respective values (0 and 1). Here is the template code:

<div v-for="(field, index) in fields">
    <div v-for="(rule, key) in field.validation">
       <select class="form-control" v-model="fields[index].validation[key].add">
         <option :value="1">Enabled</option>
         <option :value="0">Disabled</option>
       </select>
    </div>
</div>

Everything is working fine such as the v-model on select is mutating the value in the data property, but the select isn't showing the default value which is selected, here is what it looks like after rendering:

enter image description here

Can someone please tell me why isn't it working? I also tried replacing v-model with the value attribute, but no luck!

Abbas
  • 1,118
  • 1
  • 11
  • 25

2 Answers2

1

Hi I think you should try the :key attribute in the options field. Set there the value for the key. The first option should have the value 0 and the second option should have the value 1. Now there should be a mapping between the values.

<div v-for="(field, index) in fields">
    <div v-for="(rule, key) in field.validation">
       <select class="form-control" v-model="fields[index].validation[key].add">
         <option :key="1" :value="1">Enabled</option>
         <option :value="0" :value="0">Disabled</option>
       </select>
    </div>
</div>
Burak Ayyildiz
  • 325
  • 1
  • 6
0

Found the problem, it was the WIDTH! The width of the select tag was to short, hence it wasn't showing the actual text XD XD

Abbas
  • 1,118
  • 1
  • 11
  • 25