Custom template autoform not recognized


#1

Hi guys,

I have this custom template that is a slightly modified version of the one in the repo, however it does not seem to be recognized by autoform.

{{#autoForm collection="Programmes" id="insertProgrammeForm" type="insert" template="plain"}}
{{> afQuickField name="name"}}
{{> afQuickField name="description" rows=6}}
{{> afQuickField name="author"}}
{{> afQuickField name="exercises" template="myTemplate"}}
{{> afQuickField name="createdAt" type="hidden" template="myTemplate"}}
<button type="submit">Create programme</button>
{{/autoForm}}

As you can see from the above I tried to set the template directly on my quickfield with the name of exercises.

<template name="myTemplate">
<legend>Testing if it works!</legend>
{{#if afFieldIsInvalid name=this.atts.name}}
    <div class="autoform-array-field-error">
        {{{afFieldMessage name=this.atts.name}}}
    </div>
{{/if}}
{{#afEachArrayItem name=this.atts.name minCount=this.atts.minCount maxCount=this.atts.maxCount}}
    <div class="autoform-array-item">
        {{> afQuickField name=this.name label=false}}
        {{#if afArrayFieldHasMoreThanMinimum name=../atts.name minCount=../atts.minCount maxCount=../atts.maxCount}}
            <button type="button" class="autoform-remove-item">Remove</button>
        {{/if}}
    </div>
{{/afEachArrayItem}}
{{#if afArrayFieldHasLessThanMaximum name=this.atts.name minCount=this.atts.minCount maxCount=this.atts.maxCount}}
    <div style="margin-top: 20px;">
        <button type="button" class="autoform-add-item" data-autoform-field="{{this.atts.name}}" data-autoform-minCount="{{this.atts.minCount}}" data-autoform-maxCount="{{this.atts.maxCount}}">Add</button>
    </div>
{{/if}}
</template>

Is there anything special I have to do to make this work?

thanks in advance!


#2

name must be combination field type and template name according to docs


#3

Hi shock,

Do you have an example of this, or maybe a link to the relevant docs?

I’ve been looking through the docs but cannot find anything related to that


#4

#5

So I figured it out, apparently I had missed this crucial part of the docs:

To define a custom template that is recognized by the AutoForm templates system, simply create a template with the name afType + "_" + templateName. For example, if I want to define a template named "nothing" that can be used by a quickForm to generate nothing:

<template name="quickForm_nothing">
</template>

So I changed the name of my template to be afArrayField_nameOfMyTemplate and then referencing to the template directly on my afQuickField

Thanks for helping out :smile: