verbb / formie

The most user-friendly forms plugin for Craft CMS.
Other
93 stars 68 forks source link

getFieldValue() on submission throws error and I can't handle that in my template? #1885

Open jishi opened 1 month ago

jishi commented 1 month ago

Question

Deal with a submission that may or may not have a field? We have the same template rendering for multiple forms, and I need a way to just ignore a fetch of a field value if that field doesn't exist in the submit. Today it throws and renders a 500 error which is a bit odd, and I don't really know why.

Additional context

I basically do this:

{% set formSubmission = craft.formie.submissions.id(submissionId).one() %}
{% set teamName = formSubmission.getFieldValue('team') %}

This fails rendering if submission doesn't contain a team field, adding a default filter to it, still throws an error:

{% set teamName = formSubmission.getFieldValue('team')|default('') %}

And there is no real hasField() or similar. Also, I seem to lack documentation of available methods on the Submission element as well? According to documentation it has only attributes which seems wrong, it is also contradictory to your own articles.

engram-design commented 1 month ago

A 500 error should show something in your /storage/logs/formie.log if you can check?

Can you also confirm what Formie version you're on?

Calling getFieldValue('fieldHandle') will indeed throw a fatal error if that field handle doesn't exist. That's the inherited behaviour of any Craft element, like an entry.

Checking whether the field exists on an element like a Submission is a little verbose in Formie 2.x.

{% if formSubmission.fieldLayout %}
    {% if formSubmission.fieldLayout.getFieldByHandle('test') %}

Again, we're inheriting field layout behaviour from Craft's elements.

While we include the properties of a Submission object, it's incredibly time-consuming to keep a list of methods documented separately to the source code. It's easier (and more up-to-date) to check out the source code. Not to mention as this is an element, we would also need to include the base element class, which is even more involved.