1

I'm trying to create a confirmation page for a Sprout form, where I want to check if a certain field is present, and if so, check for a value, so I can return a message based on that value.

I'm using lastEntry, and am able to pull the valid fields in without a problem, but it's having issues checking whether the field is defined. I'm currently using the following code:

{% set lastEntry = craft.sproutForms.lastEntry() %}
{% set qualified = true %}
{% if lastEntry.fieldName is defined and lastEntry.fieldName == 'no' %}
    {% set qualified = false %}
{% endif %}

When I use the code above, and the field has not been used in the particular form, I see: Craft\SproutForms_EntryModel and its behaviors do not have a method or closure named "fieldName".

It appears Craft is seeing the field as defined, but not. Anyone have any ideas on what I could do to figure this out?

Thanks!

  • The check for is defined should not be necessary. The lastEntry tag returns a SproutForms_Entry model which would define all fields that you have in your Form. Also, your is defined check above could potentially return false positives in some scenarios. When you need to check is defined on Element attributes you'll want to use the syntax if lastEntry['fieldName'] is defined. (More on that issue in the Objects section here: straightupcraft.com/articles/…). – Ben Parizek Aug 5 '16 at 15:09
  • 1
    Can you share what field type you are using in your example? – Ben Parizek Aug 5 '16 at 15:10
  • Aha. That's interesting. I'm using a drop-down field in this example. – Adam Ladrach Aug 8 '16 at 17:12
0

When you set the lastEntry variable on your page. You are either setting lastEntry to be the SproutForms_FormEntryModel or null. In both cases you are defining the variable.

{# Sets lastEntry to SproutForms_FormEntryModel or null #}
{% set lastEntry = craft.sproutForms.lastEntry() %}

If you then want to test the lastEntry variable, you'll need to use something different that is defined.

I believe the following will work:

{% set lastEntry = craft.sproutForms.lastEntry() %}
{% set qualified = true %}
{% if lastEntry and lastEntry.fieldName == 'no' %}
    {% set qualified = false %}
{% endif %}

Testing for if lastEntry will work just like using if lastEntry is not null. The secondary part of your conditional should only be evaluated if if lastEntry returns true, in which case your SproutForms_FormEntryModel would exist and the fieldName field would be defined, with or without a value.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.