Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Are nested model keys supported? #28

Open
donalmurtagh opened this issue Jan 17, 2018 · 2 comments
Open

Are nested model keys supported? #28

donalmurtagh opened this issue Jan 17, 2018 · 2 comments

Comments

@donalmurtagh
Copy link

donalmurtagh commented Jan 17, 2018

I'm trying to bind fields to nested properties of the model using s simple example which you can reproduce by following these steps

  1. Create an empty app with vue init webpack vue-formly-example. When prompted, don't install eslint
  2. Install the dependencies npm install vue-formly && npm install vue-formly-bootstrap
  3. Replace the content of App.vue with the following
<template>
  <div id="app">
    <form>
      <formly-form :form="form" :model="model" :fields="fields"></formly-form>
      <button type="submit">Submit</button>
    </form>

    <div>
      <h3>Model</h3>
      <div> {{ model }}</div>
    </div>

  </div>
</template>

<script>
  import Vue from 'vue'
  import VueFormlyBootstrap from 'vue-formly-bootstrap';
  import VueFormly from 'vue-formly'

  Vue.use(VueFormly);
  Vue.use(VueFormlyBootstrap);

  export default {
    name: 'app',
    data () {
      return {
        form: {},
        model: {
          foo: {
            first: '',
            last: ''
          }
        },
        fields: [
          {key: 'foo.first', type: 'input'},
          {key: 'foo.last', type: 'input'}
        ]
      }
    }
  }
</script>

<style>
  #app {
    font-family: 'Avenir', Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-align: center;
    color: #2c3e50;
    margin-top: 60px;
  }
</style>
  1. Run the app with npm run dev and enter some data in the fields, you should then see something like this

As you can see the model keys are incorrect. Is it the case the field values can only be bound to the root of the model, or have I made a mistake?

@matt-sanders
Copy link
Contributor

No, you haven't made a mistake. Currently I haven't got that supported, but it's on the list of things to do! I'll try and get that released as soon as possible. In the meantime, something you can do, which isn't totally ideal, but does work, is something like:

model: {
   'foo.first': '',
   'foo.last': ''
}

and then use something like flat to expand it. It's not ideal, but that's the temporary fix.

@donalmurtagh
Copy link
Author

@matt-sanders thanks a lot for your swift response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants