aldeed / meteor-cfs-autoform

Upload files using CollectionFS as part of AutoForm submission
MIT License
37 stars 32 forks source link

Filetype validation blocks entire form avoiding save #25

Open dmeneses opened 9 years ago

dmeneses commented 9 years ago

Hi, i'm having problems in the validation of filetypes, not sure if is my configuration or an issue with autoform.

I have this collections:

Problems = new Meteor.Collection('problems');

ProblemsSchema = new SimpleSchema({
  title: {
      type: String,
      label: 'Nombre'
  },
  author: {
      type: String,
      label: 'Autor',
      defaultValue: 'Anónimo'
  },
  descriptionFile: {
    type: String,
    label: 'Descripcion del Problema'
  }
//More fields here...
});

Problems.attachSchema(ProblemsSchema);

ProblemDescriptionFiles = new FS.Collection('problemDescriptionFiles', {
    stores: [
      new FS.Store.FileSystem("problemDescriptions", {
        path: "~/uploads"
      })
    ],
    filter: {
      allow: {
        contentTypes: ['application/pdf'],
        extensions: ['pdf']
      },
      onInvalid: function(message) {
        if (Meteor.isClient) {
          var problemDescription = $('#problemDescription'),
            parent = problemDescription.parent(),
            span = parent.children('.help-block');
          parent.addClass('has-error');
          span.text('El archivo subido debe ser en formato PDF.');
        } else {
          console.log(message);
        }
      }
    }
  }
);

And my template follows:

<template name="problemSubmit">
  <div class="panel panel-default">
    <div class="panel-heading">
      <h3>Nuevo Problema</h3>
    </div>
    <div class="panel-body">
      {{#autoForm collection="Problems" id="insertProblemForm" type="insert"}} {{> afQuickField name='title'}} {{> afQuickField name='author'}} {{> afQuickField name='descriptionFile' id='problemDescription' type='cfs-file' collection='problemDescriptionFiles' placeholder='Ingrese la descripcion en formato pdf'}}

      <h2>Restricciones</h2>
      <div class="col-md-6 col-sm-6">
        <div class="form-group{{#if afFieldIsInvalid name='timeConstraint'}} has-error{{/if}}">
          <div class="input-group">
            {{> afFieldInput name='timeConstraint'}}
            <span class="input-group-addon">Milisegundos</span>
          </div>
          {{#if afFieldIsInvalid name='timeConstraint'}}
          <span class="help-block">{{afFieldMessage name='timeConstraint'}}</span> {{/if}}
        </div>
      </div>

      <div class="col-md-6 col-sm-6">
        <div class="form-group{{#if afFieldIsInvalid name='memoryConstraint'}} has-error{{/if}}">
          <div class="input-group">
            {{> afFieldInput name='memoryConstraint'}}
            <span class="input-group-addon">Bytes</span>
          </div>
          {{#if afFieldIsInvalid name='memoryConstraint'}}
          <span class="help-block">{{afFieldMessage name='memoryConstraint'}}</span> {{/if}}
        </div>
      </div>
      <!--{{> afQuickField name='solutionSizeConstraint'}}-->

      <h2>Pruebas</h2> {{> afQuickField name='inputTest' type='cfs-file' collection='problemFiles' placeholder='Ingrese el archivo con casos de entrada'}} {{> afQuickField name='outputTest' type='cfs-file' collection='problemFiles' placeholder='Ingrese el archivo con las salidas esperadas'}}
      <button type="submit" class="btn btn-default">Crear</button>
      {{/autoForm}}
    </div>
  </div>
</template>

Since I added the validation for the descriptionFile (PDF) is throwing an exception when the file is not a PDF. But only is reproducible when i fill all the fields correctly and i put in the descriptionFile field a non PDF file. So this exception is thrown and the save button keeps disabled no matter what change i do in the form.

debug.js:41 Exception in setTimeout callback: Error: Cannot remove a file that is not associated with a collection
    at FS.File.remove (http://localhost:3000/packages/cfs_file.js?f44220407bc5afa9d512bd07e33dcd3dde0839b9:371:14)
    at http://localhost:3000/packages/cfs_autoform.js?ca0ee21422d608dd4c429aa4c230839a532b1cd9:331:9
    at Array.forEach (native)
    at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:156:11)
    at HTMLInputElement.<anonymous> (http://localhost:3000/packages/cfs_autoform.js?ca0ee21422d608dd4c429aa4c230839a532b1cd9:330:7)
    at Function.jQuery.extend.each (http://localhost:3000/packages/jquery.js?dd8bac56f8fd3666d433d2285ae01e52597cc51a:417:23)
    at jQuery.fn.jQuery.each (http://localhost:3000/packages/jquery.js?dd8bac56f8fd3666d433d2285ae01e52597cc51a:169:17)
    at Object.CfsAutoForm.deleteUploadedFiles (http://localhost:3000/packages/cfs_autoform.js?ca0ee21422d608dd4c429aa4c230839a532b1cd9:328:31)
    at cb (http://localhost:3000/packages/cfs_autoform.js?ca0ee21422d608dd4c429aa4c230839a532b1cd9:180:23)
    at http://localhost:3000/packages/cfs_autoform.js?ca0ee21422d608dd4c429aa4c230839a532b1cd9:220:13