<p>I have updated the plugin files to make this work as I needed it to.</p>

<p>In <code>sproutforms/variables/SproutFormsVariables.php</code>, I have updated the <code>$bodyHtml</code> variable at Line 57 to include <code>handle =&gt; $formHandle</code></p>

<pre><code>$bodyHtml = craft()-&gt;templates-&gt;render(
    'tab', array(
        'handle'               =&gt; $formHandle,
        'tabs'                 =&gt; $form-&gt;getFieldLayout()-&gt;getTabs(),
        'entry'                =&gt; $entry,
        'formFields'           =&gt; $fields,
        'thirdPartySubmission' =&gt; !!$form-&gt;submitAction,
        'displaySectionTitles' =&gt; $form-&gt;displaySectionTitles,
        'renderingOptions'     =&gt; $renderingOptions
    )
); 
</code></pre>

<p>This means in my Template Override in <code>_forms/tabs.html</code> I can pass the <code>handle</code> as a variable</p>

<pre><code>{%- if formField -%}
    {% include "field" with {
        form:                 handle,
        field:                field,
        element:              entry,
        required:             required,
        formField:            formField,
        renderingOptions:     renderingOptions,
        thirdPartySubmission: thirdPartySubmission,
    } only %}
{%- endif %}  
</code></pre>

<p>I can then do as I required with not needing to specifically specify a form name in my <code>submittedEntry</code> variable</p>

<pre><code>{% set ourForm = craft.sproutForms.form(form) %}
{% set submittedEntry = craft.sproutForms.getEntry(ourForm) %}
</code></pre>

<hr>

<p>The use case for this update is if clients can add forms through the Matrix, you need to get <code>form.handle</code> in <code>submittedEntry</code> so that you can populate the form post using <code>value="{{ attribute(submittedEntry, fieldName) }}</code> if the form fails validation (so that the form doesn't return empty on validation failure).</p>

<p>In the documentation the <code>submittedEntry</code> value is always manually set, however in this example you need to be able to set <code>submittedEntry</code> dynamically so that you can use generic Template Overrides in <code>_forms</code> rather than having overrides per form in <code>_forms/formHandle</code>.</p>

CC BY-SA 3.0
2016-08-30T15:56:38.423Z
2016-09-01T10:33:33.830Z
2016-09-01T10:33:33.830Z
TWJ
<p>I've created a custom field.html which is working fine when I manually specify the formHandle for use with <code>submittedEntry</code> as mentioned in the <a href="http://sprout.barrelstrengthdesign.com/craft-plugins/forms/docs/examples/custom-html" rel="nofollow">docs</a>:</p>

<pre><code>{% set ourForm = craft.sproutForms.form('formHandle') %}
{% set submittedEntry = craft.sproutForms.getEntry(ourForm) %}
</code></pre>

<p>However, I need to be able to have a dynamic formHandle in the field.html as I am using the same file for an infinite number of forms. This means I cannot manually specify the formHandle as it throws the below error (as the form methods for <em>Form A</em> won't exist in <em>Form B</em>)</p>

<pre><code>Craft\SproutForms_EntryModel and its behaviors do not have a method or closure named "fieldNameFormB".
</code></pre>

<hr>

<p>Is there any way I can pass <code>form.handle</code> from <em>form.html</em> into <em>field.html</em> so that I can use the below code, or is there another method by which I'm not forced to manually specify the form?</p>

<pre><code>{% set ourForm = craft.sproutForms.form(form.handle) %}
{% set submittedEntry = craft.sproutForms.getEntry(ourForm) %}
</code></pre>

CC BY-SA 3.0
2016-08-30T15:21:03.863Z
The error shown does not relate to a `SproutForms_FormModel`. It relates to a `SproutForms_EntryModel`. That `SproutForms_EntryModel` shouldn't exist until a form is submitted and fails validation. It's not clear to me how the error on the `SproutForms_EntryModel` relates to your question about dynamically handling the `form.handle`.
Ben Parizek
CC BY-SA 3.0
2016-08-30T15:24:04.030Z
Also, what context are you in that you are trying to display forms without knowing the name of the form that you wish to display? Will your forms be identified via Form Relations fields?  If so, have you considered using Template Overrides (http://sprout.barrelstrengthdesign.com/craft-plugins/forms/docs/customization/template-overrides) instead of custom the HTML and dynamically passing in the form handle as it exists from the Form Relations field (http://sprout.barrelstrengthdesign.com/craft-plugins/forms/docs/examples/relations-field-on-entry-page)?
Ben Parizek
CC BY-SA 3.0
2016-08-30T15:25:42.717Z
The code throws that error when I try to hardcode the formHandle, because I am using `value="{{ attribute(submittedEntry, fieldNameFormB) }}"` like in the docs, and `fieldNameFormB` doesn't exist in `submittedEntry` as `submittedEntry` is looking at *Form A* which doesn't have that field defined.
TWJ
CC BY-SA 3.0
2016-08-30T15:30:48.830Z
I am using Template Overrides, however we still need custom HTML within fields.html to allow us to style the form in line with client guidelines. The forms are specified in the Content Matrix and I am using `{{ craft.sproutForms.displayForm(block.customForm.first.handle) }}` to display the form through the Matrix. Hence we are not hardcoding in the `formHandle` as the client may generate forms themselves to put into their pages through the Matrix.
TWJ
CC BY-SA 3.0
2016-08-30T15:58:27.567Z
Ben, see my answer to this question. I have updated the `SproutFormsVariables.php` in the plugin to pass the form handle for use within `tabs.html`, which allows me to then use this in `fields.html`. I'm not sure if this is something you would want to add to a future update (would be nice so that I don't have to remember to update this any time you do an update!), but I have explained it a bit more in-depth. Thanks for the help!
TWJ
CC BY-SA 3.0
2016-08-30T16:59:55.650Z
Thanks for explaining things in more detail and sharing what it took to get working for you.  Aside from preference, which I think is a fine answer, is there a reason this can't just be addressed by using different css or js class names?  For example something like `form.formHandle input { ... }` instead of `input.formHandle { ... }`?
Ben Parizek
CC BY-SA 3.0
2016-08-31T08:10:47.187Z
Needing `form.handle` in `field.html` wasn't to do with styling, we needed this functionality as clients can add forms through the Matrix, and to get the validation to work correctly we need to get the entry (in `submittedEntry`) so the we can populate the post (so if the form fails validation, people do not lose all of their already filled information). In your documentation you are always manually setting `submittedEntry` with a form name, however we needed to be able to set `submittedEntry` dynamically so that we could use generic Template Overrides rather than having overrides per form.
TWJ
CC BY-SA 3.0
2016-08-31T15:22:47.047Z
Got it. Thanks for taking the time to explain further. We'll look into addressing this use case.
Ben Parizek
CC BY-SA 3.0
2016-09-01T10:36:17.633Z
Thanks Ben, I've updated my answer to include the use case explanation.
TWJ
<form><plugin-sproutforms>
How to use form.handle in Custom field.html
# How to use form.handle in Custom field.html

(Asked by: username_0 on 2016-08-30)

I've created a custom field.html which is working fine when I manually specify the formHandle for use with submittedEntry as mentioned in the docs:
{% set ourForm = craft.sproutForms.form('formHandle') %}
{% set submittedEntry = craft.sproutForms.getEntry(ourForm) %}

However, I need to be able to have a dynamic formHandle in the field.html as I am using the same file for an infinite number of forms. This means I cannot manually specify the formHandle as it throws the below error (as the form methods for Form A won't exist in Form B)
Craft\SproutForms_EntryModel and its behaviors do not have a method or closure named "fieldNameFormB".


Is there any way I can pass form.handle from form.html into field.html so that I can use the below code, or is there another method by which I'm not forced to manually specify the form?
{% set ourForm = craft.sproutForms.form(form.handle) %}
{% set submittedEntry = craft.sproutForms.getEntry(ourForm) %}

**Tags:** form, plugin-sproutforms

> **Comments:**
> **username_1**: The error shown does not relate to a `SproutForms_FormModel`. It relates to a `SproutForms_EntryModel`. That `SproutForms_EntryModel` shouldn't exist until a form is submitted and fails validation. It's not clear to me how the error on the `SproutForms_EntryModel` relates to your question about dynamically handling the `form.handle`.  
> **username_1**: Also, what context are you in that you are trying to display forms without knowing the name of the form that you wish to display? Will your forms be identified via Form Relations fields?  If so, have you considered using Template Overrides (http://sprout.barrelstrengthdesign.com/craft-plugins/forms/docs/customization/template-overrides) instead of custom the HTML and dynamically passing in the form handle as it exists from the Form Relations field (http://sprout.barrelstrengthdesign.com/craft-plugins/forms/docs/examples/relations-field-on-entry-page)?  
> **username_0**: The code throws that error when I try to hardcode the formHandle, because I am using `value="{{ attribute(submittedEntry, fieldNameFormB) }}"` like in the docs, and `fieldNameFormB` doesn't exist in `submittedEntry` as `submittedEntry` is looking at *Form A* which doesn't have that field defined.  
> **username_0**: I am using Template Overrides, however we still need custom HTML within fields.html to allow us to style the form in line with client guidelines. The forms are specified in the Content Matrix and I am using `{{ craft.sproutForms.displayForm(block.customForm.first.handle) }}` to display the form through the Matrix. Hence we are not hardcoding in the `formHandle` as the client may generate forms themselves to put into their pages through the Matrix.  
> **username_0**: Ben, see my answer to this question. I have updated the `SproutFormsVariables.php` in the plugin to pass the form handle for use within `tabs.html`, which allows me to then use this in `fields.html`. I'm not sure if this is something you would want to add to a future update (would be nice so that I don't have to remember to update this any time you do an update!), but I have explained it a bit more in-depth. Thanks for the help!  
> **username_1**: Thanks for explaining things in more detail and sharing what it took to get working for you.  Aside from preference, which I think is a fine answer, is there a reason this can't just be addressed by using different css or js class names?  For example something like `form.formHandle input { ... }` instead of `input.formHandle { ... }`?  
> **username_0**: Needing `form.handle` in `field.html` wasn't to do with styling, we needed this functionality as clients can add forms through the Matrix, and to get the validation to work correctly we need to get the entry (in `submittedEntry`) so the we can populate the post (so if the form fails validation, people do not lose all of their already filled information). In your documentation you are always manually setting `submittedEntry` with a form name, however we needed to be able to set `submittedEntry` dynamically so that we could use generic Template Overrides rather than having overrides per form.  
> **username_1**: Got it. Thanks for taking the time to explain further. We'll look into addressing this use case.  
> **username_0**: Thanks Ben, I've updated my answer to include the use case explanation.  

---

## 1 answers

### Answer by username_0 on 2016-08-30. Score: 1 | Accepted answer

I have updated the plugin files to make this work as I needed it to.
In sproutforms/variables/SproutFormsVariables.php, I have updated the $bodyHtml variable at Line 57 to include handle => $formHandle
$bodyHtml = craft()->templates->render(
    'tab', array(
        'handle'               => $formHandle,
        'tabs'                 => $form->getFieldLayout()->getTabs(),
        'entry'                => $entry,
        'formFields'           => $fields,
        'thirdPartySubmission' => !!$form->submitAction,
        'displaySectionTitles' => $form->displaySectionTitles,
        'renderingOptions'     => $renderingOptions
    )
); 

This means in my Template Override in _forms/tabs.html I can pass the handle as a variable
{%- if formField -%}
    {% include "field" with {
        form:                 handle,
        field:                field,
        element:              entry,
        required:             required,
        formField:            formField,
        renderingOptions:     renderingOptions,
        thirdPartySubmission: thirdPartySubmission,
    } only %}
{%- endif %}  

I can then do as I required with not needing to specifically specify a form name in my submittedEntry variable
{% set ourForm = craft.sproutForms.form(form) %}
{% set submittedEntry = craft.sproutForms.getEntry(ourForm) %}


The use case for this update is if clients can add forms through the Matrix, you need to get form.handle in submittedEntry so that you can populate the form post using value="{{ attribute(submittedEntry, fieldName) }} if the form fails validation (so that the form doesn't return empty on validation failure).
In the documentation the submittedEntry value is always manually set, however in this example you need to be able to set submittedEntry dynamically so that you can use generic Template Overrides in _forms rather than having overrides per form in _forms/formHandle.


---

