1

I have a custom donation form that I'm using that includes some custom Craft fields. Two of those fields are dropdowns which I populated with options from within my Craft admin panel.

Since this is a custom form, it must use Stripe.js, so I have the following lines in my template per the tutorial on the SquareBit site:

{% includeJsResource('charge/js/stripe_v2.min.js') %}
{% includeJsResource('charge/js/jquery.charge.js') %}
{% includejs %}
    $(this).charge();
{% endincludejs %}

Here are my Payment options that are set inside the <form> declaration:

{% set chargePaymentOptions = { 'allowDynamic' : true, 'planCurrency': 'usd', } %}

{{ craft.charge.setPaymentOptions(chargePaymentOptions) }}

I borrowed some code from the Charge plugin examples for initializing the select fields like so:

{{ forms.selectField({
     label: 'Donation Recurrence',
     name: 'donationRecurrence',
     id: 'donationRecurrence',
     options: (charge is defined ? charge.donationRecurrence.options),
     value: (charge is defined ? charge.donationRecurrence),
     errors: (charge is defined ? charge.getErrors('donationRecurrence'))
}) }}

{{ forms.selectField({
     label: 'Would you like to donate to a specific cause?',
     name: 'donationDesignation',
     id: 'donationDesignation',
     options: (charge is defined ? charge.donationDesignation.options),
     value: (charge is defined ? charge.donationDesignation),
     errors: (charge is defined ? charge.getErrors('donationDesignation'))
}) }}

As you can see, the select fields get their options from the aforementioned pre-populated custom Craft fields. Those options do not show up on first page load, but if the form is subsequently submitted and comes back with errors, then the select field options appear.

I figured out that on the first page load, doing a dump(charge) shows that the charge object does not exist until the page has been reloaded after the form was submitted.

Thanks for any help.

2
  • My current workaround, get the field directly and set this to the select field options: {% set donationRecurrenceOptions = craft.fields.getFieldByHandle('donationRecurrence').settings.options %}
    – tofirius
    Commented Jan 4, 2017 at 19:26
  • Would you mind adding your workaround as an answer, @tofirius?
    – Brad Bell
    Commented Jan 4, 2017 at 22:06

1 Answer 1

1

My current workaround: Get the select field directly and set its options to the select field options:

{% set donationRecurrenceOptions = craft.fields.getFieldByHandle('donationRecurrence').settings‌​.options %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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