Open thomascoppein opened 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,
}) %}
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?
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.
Sounds great, I'm on the latest version of Formie: 3.0.0-beta.19. Thanks in advance for the great support!
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!
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!
Additional context
No response