4

I am trying to create a Price Set with multiple fields, each with multiple options, and require one option from any of the fields. Is it possible to make the Price Set itself required, or can you only make individual fields required?

We have multiple pricing options for Adults, Youth, and Children so each category is its own field. I don't want to make the fields required since a single person isn't an Adult and Youth and Child. However, if I include 'none' options then it is possible for someone to have no selection at all. It appears this question has been asked before but there was no solution back then. I'm hoping there is one now.

3 Answers 3

5

As long as there’s a fee for all options then try this method –

When editing or creating a price set, enter a value in the ‘Minimum Amount’ field. In this example, I’ve entered 5, as that’s the smallest fee available for this event.

Minimum Amount

None of the price fields have ben set to required -

Not Required

If a participant attempts to select ‘none’ for all of the options, they will be presented with the below message and will not be able to proceed -

Alert Message

2
  • That's an interesting idea just that would allow picking e.g. BOTH adult and youth options, whereas it sounds like they only want them to pick one option out of all.
    – Demerit
    Jun 17, 2022 at 17:33
  • That may work out. As mentioned, someone could potentially choose multiple options, but that's less of an issue.
    – PoHo
    Jun 17, 2022 at 20:55
2

Can you just put all the options into one field? Are you separating them into multiple fields purely for visual reasons? If so, then maybe you can use some custom css to do that, e.g. div.price-set-row.fieldname-row3 { margin-top: 1em; }

1
  • This is not ideal but is my backup plan if there's no other solutions that work for us
    – PoHo
    Jun 17, 2022 at 20:54
2

If you need something more custom, and you are not afraid to do a bit of code, you can always add a validation using hook_civicrm_validateForm (and optionally using javascript).

For hook_civicrm_validateForm, you need to create an extension with the function that looks like :

function myextension_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {

  if ($formName == 'CRM_Event_Form_Registration_Register') {

    $formId = $form->getVar('_id');
    // event id in this case
    if ($formId == 1) {

      // add whatever logic you want
      $price1 = CRM_Utils_Array::value('price_1', $fields);
      $price2 = CRM_Utils_Array::value('price_2', $fields);
      $price3 = CRM_Utils_Array::value('price_3', $fields);

      if (empty($price1) && empty($price2) && empty($price3)) {
        $errors["price_1"] = ts('You must choose one price item');
      }

    }

  }
}
1
  • Thank you. Unfortunately I am not personally confident enough in my coding skills that I'd feel comfortable making this change, but it's good to know that it's possible. This could be helpful to me, and others, in the future though when I feel like I can do that.
    – PoHo
    Jun 29, 2022 at 17:54

Your Answer

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

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