memele
4 months ago

How to save multiple models from one form

Posted 4 months ago by memele

I am building a resume generator. I have a form where the user fills out basic data about themselves and a section with info about education. The user can append multiple fields about education with jQuery. The question is - how do I create a foreach loop to save all the information? I know how to do this with one field but how do I do this with multiple fields and have them all saved as a separate Education model instance? This is the html for each section for education:

    <div class="form-group education-container">
                    <div class="education-wrapper">
                        <div class="form-group">
                            <div class="form-row">
                                <div class="col-6">
                                    <div class="form-group">
                                        <label for="education-title">Title of Institution</label>
                                        <input type="text" class="form-control education-title" name="education-title[]" id="education-title" placeholder="Title of Institution" required>
                                    </div>
                                </div>

                                <div class="col-6">
                                    <div class="form-group">
                                        <label for="education-specialty">Specialty</label>
                                        <input type="text" class="form-control education-specialty" name="education-specialty[]" id="education-specialty" placeholder="Specialty" required>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="form-group">
                            <div class="form-row">
                                <div class="col-6">
                                    <label for="education-year-from">Year From</label>
                                    <select class="form-control education-year-from" name="education-year-from[]" id="education-year-from" required>
                                        <option>Year from..</option>
                                        <option>2</option>
                                        <option>3</option>
                                        <option>4</option>
                                        <option>5</option>
                                    </select>
                                </div>
                                <div class="col-6">
                                    <label for="education-year-to">Year to</label>
                                    <select class="form-control education-year-to" name="education-year-to[]" id="education-year-to" required>
                                        <option>Year to..</option>
                                        <option>2</option>
                                        <option>3</option>
                                        <option>4</option>
                                        <option>5</option>
                                        <option> - now</option>
                                    </select>
                                </div>
                            </div>
                        </div>
                    </div>
                    

                </div>
                <button class="btn btn-success add-education">+</button>

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