verbb / formie

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

Disable radio buttons based on previous submissions #1991

Open thomascoppein opened 1 month ago

thomascoppein commented 1 month ago

Question

Is there a straightforward method to disable radio buttons in a Craft CMS form based on the number of previous submissions selecting those options? Simply said: setting a limit on the number of times each option in a radio button field can be selected. Refer to the screenshot for a visual representation.

Thanks in advance!

Screenshot 2024-07-25 at 17 13 08

Additional context

No response

engram-design commented 1 month ago

It's not out-of-the-box, but yes this could be done. You'd need to first look up previous submissions for the form:

{% set submissions = craft.formie.submissions().form('contactForm').all() %}

Then get all the values for the Radio Button field in question:

{% set timeslots = [] %}

{% for submission in submissions %}
    {% set timeslots = timeslots | merge([ submission.myRadioButtonHandle ]) %}
{% endfor %}

Then, you can modify the options of the Radio Button field.

{% set options = submission.getFieldByHandle('myRadioButtonHandle').options %}
{% set newOptions = [] %}

{% for option in options %}
    {% if option.handle in timeslots %}
        {% set newOptions = newOptions | merge([{ label: option.label, value: option.value, disabled: true }]) %}
    {% else %}
        {% set newOptions = newOptions | merge([{ label: option.label, value: option.value, disabled: false }]) %}
    {% endif %}
{% endfor %}

{% do form.setFieldSettings('myRadioButtonHandle', {
    options: newOptions,
}) %}
thomascoppein commented 1 month ago

Thanks, that’s a great start. The only issue I’m currently facing is how to set a limit per option in the dashboard. Is it possible to add an extra field to the existing field settings in Formie, or should I create a custom field for this?

engram-design commented 1 month ago

You won't be able to add extra field settings to a core field, but you can create your own! In fact, you could extend the Formie Radio Button class itself, and just add your settings to the "schema" (what defines the UI for field settings).

Let me know what Formie version you're on, and I can send through an example.

thomascoppein commented 1 month ago

Sounds great, I'm on the latest version of Formie: 3.0.0-beta.19. Thanks in advance for the great support!

engram-design commented 1 month ago

A great first start is our user guide about custom fields to gain a full understanding of things.

In your case, you'll extend an existing class, and extend one of the settings screens.

<?php
namespace modules\mymodule;

use Craft;
use verbb\formie\fields\Radio;
use verbb\formie\helpers\SchemaHelper;

class CustomRadio extends Radio
{
    public ?string $myProperty = null;

    public function defineGeneralSchema(): array
    {
        $schema = parent::defineGeneralSchema();

        $schema[] = SchemaHelper::textField([
            'label' => Craft::t('formie', 'Some Field'),
            'name' => 'myProperty',
        ]);

        return $schema;
    }
}

Here, you create a property myProperty, and then using the SchemaHelper you can create an appropriate field to edit your setting. I've chosen a simple text field. There's lots of options available to you covered in the docs.

Hope that helps!