am-impact / amforms

Forms plugin for Craft
Other
122 stars 21 forks source link

Custom field issue #155

Open CptCompiler opened 6 years ago

CptCompiler commented 6 years ago

Hi, thank you for your great plugin.

I want to make a custom field like this: <input type="text" id="frm_comment" name="fields[comment]" value="{% if formHandle.comment is defined %}{{ formHandle.comment }}{% endif %}">

I just don't know what you mean with: "Change formHandle to your form's handle". In the CP I set the handle of my form to "contactForm". Should I edit the code so it looks like this?

<input type="text" id="frm_comment" name="fields[comment]" value="{% if contactForm.comment is defined %}{{ contactForm.comment }}{% endif %}">

When I dump "contactForm" I get an undefined variable. So this is probably not what you mean, is it?

What am I missing?

hubertprein commented 6 years ago

Possibly, you have to do: <input type="text" id="frm_comment" name="fields[comment]" value="{% if contactForm is defined and contactForm.comment is defined %}{{ contactForm.comment }}{% endif %}">

The name of the field possibly needs a namespace as well. Check the source (DOM HTML) from your other fields to see what you need to change in your custom field HTML.

CptCompiler commented 6 years ago

Hi, thank you for your help. Actually my code looks like this:

{{ craft.amForms.displayForm('contactForm') }}

The form template:

{% set form = craft.amForms.getForm('contactForm') %}

<form method="post" action="" accept-charset="UTF-8">
    {{ getCsrfInput() }}

    {# This should always be here! #}
    <input type="hidden" name="action" value="amForms/submissions/saveSubmission">

    {# Insert your form's handle. #}
    <input type="hidden" name="handle" value="{{ form.handle }}">

    {# This will namespace your inputs (for IDs and such), but it's not required though #}
    <input type="hidden" name="namespace" value="{{ form.getNamespace() }}">

    {# Place the HTML of your fields here #}

    {{ form.displayField('mobil') }}

    <input placeholder="08:00" style="width:100px; margin-left: 20px; margin-right: 20px;" class="FormField__Input" type="text" id="frm_von" name="{{ form.getNamespace() }}[fields][von]" value="{% if contactForm.von is defined %}{{ contactForm.von }}{% endif %}">

    <button style="margin-top:20px" type="submit" class="PrimaryCta PrimaryCta--green PrimaryCta--textCenter PrimaryCta--fullWidthXS">Absenden</button>

</form>

In the DOM it looks like this:

<input type="hidden" name="handle" value="contactForm"> <input type="hidden" name="namespace" value="form_SQUnC8M5Rr"> ... <input class="FormField__Input text nicetext fullwidth" type="text" id="form_SQUnC8M5Rr-fields-mobil" name="form_SQUnC8M5Rr[fields][mobil]" value="" data-show-chars-left="" autocomplete="off" placeholder=""> ... <input placeholder="08:00" style="width:100px; margin-left: 20px; margin-right: 20px;" class="FormField__Input" type="text" id="frm_von" name="form_SQUnC8M5Rr[fields][von]" value="">

contactForm is never defined. I can send the form but when there is a form error and the page is reloaded with error messages the "von" field does not have the previously entered values.

hubertprein commented 6 years ago

I have no idea what's going on, on your end, but I've tested it with this custom HTML: <input type="text" name="{{ form.getNamespace() }}[fields][von]" value="{% if contactForm.von is defined %}{{ contactForm.von }}{% endif %}"> As you can see in your example, both our field name and value if statement, is the same.

I have created the form with the same handle as you did. Two fields in the field layout, firstName and von. I've set firstName to be required, and don't include it in the custom HTML, so the page reloads and throws an error. My von field, is getting the value I've given in, just fine.

Make sure you've added the {{ craft.amForms.displayAntispam() }} though. Your stuff could be marked as spam. Because you have no redirect in there, you redirect to the same page, looking like it wasn't sent.

CptCompiler commented 6 years ago

I don't get it. Where is the "contactForm" variable set? Or do I have to set it somewhere like I'm setting the form variable {% set form = craft.amForms.getForm('contactForm') %}?

MattWilcox commented 5 years ago

Having exactly the same issue - this is where a full working code example of the entire form with a custom HTML field would clear up this hopeless ambiguity.

MattWilcox commented 5 years ago

Here's the crux of it: {% if contactForm is defined ... %}.

It isn't. So how are we supposed to be doing that because the docs just say to write the name of the form we want - but doing that results in just a string that's the name of the form and so has no functionality.