Search code examples
meteormeteor-autoform

AutoForm.getFieldValue for array fields


I have a schema declared as:

JobSchema = new SimpleSchema({
    responsibilities: {
        type: [String],
        optional: true
    },
    'responsibilities.$': {
        min: 2,
        autoform: {
            afFieldInput: {
                class: 'form-control'
            },
            placeholder: 'E.g. "Build tools according to specifications"'
        }
    }
});

Also I have an UI helper declared as:

Template.registerHelper('currentFieldValue', function (fieldName) {
    return AutoForm.getFieldValue('insertJobForm', fieldName) || '';
});

I have a template where I used this helper to generate a form preview. It works like charm for all fields except the array ones. Nothing is being rendered. Any ideas?

{{# if currentFieldValue "responsibilities"}}
    <h3>Responsibilities</h3>
    {{{currentFieldValue "responsibilities"}}}
{{/if}}

Solution

  • A quick and dirty workaround:

    job_create.js

    Template.jobCreate.helpers({
        responsibilities: function() {
            var formData = AutoForm.getFormValues('insertJobForm');
            return formData.insertDoc.responsibilities || [];
        }
    });
    

    job_create.html

    {{# if responsibilities}}
        <h3>Responsibilities</h3>
        {{responsibilities}}
    {{/if}}