theUnforgiven
209
14
Vue

Help with name field w/VueJS saving as array?

Posted 1 week ago by theUnforgiven

I have the following form in a Vue file:

<div v-for="(input, index) in inputs" :key="index">
            <div class="row">
                <div class="col">
                    <div class="form-group">
                        <input type="text" 
                            class="form-control" 
                            :id="input.id" 
                            v-model="input.item" 
                            placeholder="Enter any new fees here"
                        >
                    </div>
                </div>

                <div class="col">
                    <div class="form-group">
                        
                        <div class="input-group mb-3">
                            <div class="input-group-prepend">
                              <div class="input-group-text">£</div>
                            </div>
                            <input type="number" 
                                class="form-control" 
                                placeholder="0.00" 
                                :id="input.id" 
                                v-model="input.amount" 
                            >
                            <button v-on:click.prevent="removeInput(input.id)" class="btn btn-sm btn-danger" onclick="return confirm('Are you sure you want to remove this?')"><i class="fa fa-trash"></i></button>
                        </div>   
                    </div>
                </div>
            </div>
        </div>

Once the form is submitted I want to have name values like this is from another element within the same form. Basically add to this array with what the user types in the first input and then the fee amount in the amount field.

 "fees" => array:4 [▼
    "admin" => "10.00"
    "reminder" => "40.00"
    "bank" => "50.00"
  ]

I did try :name="'fees[' + input.item + ']'" but this results in:

"fees" => array:5 [▼
    "admin" => "10.00"
    "reminder" => "40.00"
    "bank" => "50.00"
    "new item" => "new item" // wrong
    0 => "99" // wrong
    // should be 
    "new item" => "99"
  ]

Can anyone help me on what the name field should be please J

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.