Meteor-Community-Packages / meteor-autoform

AutoForm is a Meteor package that adds UI components and helpers to easily create basic forms with automatic insert and update events, and automatic reactive validation.
MIT License
1.44k stars 328 forks source link

afArrayField reactivity bug because of name property on element removal and add #1533

Closed horodnicdragos closed 3 years ago

horodnicdragos commented 8 years ago

I am using afArrayField with two fields using properties from the same collection (Fa). The collection has nested values and on the second field of the form (fi.$.uni), it returns an option list depending on the selected value from the first field (fi.$.fac).

Everything works well, until I remove on of the array's elements by clicking the '-' button. Afterwards the second field will not show the corresponding options. I did find something that I hope to be helpful.

First of all, here is my schema:

Schemas.CV = new SimpleSchema(
  fi: type: [ Object ]
  'fi.$.uni':
    type: String
    autoform: options: ->
      Fa.find().fetch().map (obj) ->
        {
          label: obj.title
          value: obj.title
        }
  'fi.$.fac':
    type: String
    autoform: options: ->
      uni = AutoForm.getFieldValue(@name.replace('.fac', '.uni'))
      console.log @name
      if !uni
        []
      else
        Fa.findOne(title: uni).fac.map (obj) ->
          {
            label: obj
            value: obj
          }
)

Template:

{{> afArrayField name='fi' fields="fi.$.uni, fi.$.fac"}}

After some brief debugging. it appears that @name does not update on array removal. Here is an example: I have the following name values for two elements in the array:

fi.0.fac fi.1.fac

I add another one:

fi.0.fac fi.1.fac fi.2.fac

I remove the middle element and what remains is:

fi.0.fac fi.2.fac

Adding another element will result in:

fi.0.fac fi.2.fac fi.3.fac

I am pretty sure this is the cause of not being able to reactively alter options list after removing an element from the array.

rafinskipg commented 7 years ago

Hi @horodnicdragos I have experienced the same issue when creating my custom templates. I cannot access the AutoForm.getFieldValue correctly because the template.data.name property does not update.

If you find any solution or clue, please, let me know. Thanks