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

Adding array element and then deleting first element breaks validation. BUG #707

Closed
jcheroske opened this issue Feb 19, 2015 · 7 comments
Closed

Comments

@jcheroske
Copy link

Given a schema like:

Contacts.attachSchema(new SimpleSchema({

   name: {
      type: String,
      label: 'Name',
      max: 40
   },
   methods: {
      type: [Object],
      minCount: 1
   },
   'methods.$.type': {
      type: String,
      allowedValues: ['email','phone'],
      autoform: {
         options: [
            { label: 'Email', value: 'email' },
            { label: 'Phone', value: 'phone' }
         ]
      }
   },
   'methods.$.email': {
      type: String,
      regEx: SimpleSchema.RegEx.Email,
      max: 50
   },
   'methods.$.phone': {
      type: String,
      min: 7,
      max: 25
   },
   description: {
      type: String,
      label: 'Description',
      max: 1000,
      autoform: {
         rows: 10
      }
   }

}));

SimpleSchema.messages({
   required: "[label] is required",
   minString: "[label] must be at least [min] characters",
   maxString: "[label] can not exceed [max] characters",
   regEx: [
      { exp: SimpleSchema.RegEx.Email, msg: "[label] must be a valid email address" }
   ]
});

If I have a simple form like:

           {{# autoForm collection="Contacts" id="insertContactForm" type="insert" template="bviw" validation="keyup"}}
              {{> afFormGroup name="name" template="bviw"}}
              {{> afArrayField name="methods" template="bviw"}}
              {{> afFormGroup name="description" template="bviw"}}
           {{/autoForm}}

And I add a field to the array and then delete the first array element, validators no longer run. If I make the array three elements long, and then delete the middle element, validators still run on the first element but not on the second. I've tried using the bootstrap theme as well. There seems to be a bug when the array has missing elements. The validators only run on array elements before the first missing element.

@jcheroske
Copy link
Author

I've spent some more time looking into this. What I've found, which may just be expected behavior, is that an array of objects is not validated on keyup validation within an insert form. If I have a schema that defines an array field containing objects, then no validation of the object's fields will occur during validation prior to form submission. If I add defaultValue: [{}] to the schema definition of the array, then the first object will validate. Any additional objects will not, I assume because they don't exist yet. Here is a meteorpad to illustrate:

http://meteorpad.com/pad/AS5zqm6iWDcPkDZD8/Autoform%20Array%20Validation%20Bug

So this issue may boil down to simply this: when adding a new element to an array using autoform-add-item, is there a way to tell autoform (or simple-schema) to create a new, empty object, when the new array element is added, so that validation will then occur?

@jcheroske
Copy link
Author

Please go to the pad above and do the following:

  1. Type a character into the 'name' field of array[0]. You will see a validation error.

  2. Press '+' to add another element to the array.

  3. Type a character into the 'name' field of the array[1]. You will see another validation error.

  4. Press the '-' button to remove element [0]. You should see the validation error of the array[1] field disappear.

  5. Mess around with the remaining name field. You should see that it no longer validates.

@serkandurusoy
Copy link

I removed my earlier comments here. I think I had under-tested. #1289 is on the right track to solve this and I updated autovalues for my array types to return _.compact(this.value) and it resolves the issue.

@cesarve77
Copy link

+1

3 similar comments
@rafinskipg
Copy link

+1

@felixcatto
Copy link

+1

@chenhuiyi
Copy link

+1

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

No branches or pull requests

8 participants