surveyjs / survey-library

Free JavaScript form builder library with integration for React, Angular, Vue, jQuery, and Knockout.
https://surveyjs.io/form-library
MIT License
4.22k stars 816 forks source link

Validate all checkboxes are checked from restful service #8972

Closed gregpawlowski closed 3 weeks ago

gregpawlowski commented 1 month ago

I'm not sure if this is a new feature request or a bug. But I would like to ensure that all checkboxes that were present as choices for a checkbox question from a restful service are checked. Currently it appears one would need to know the number of choices present in order to validate they are all checked off.

Solution: For a restful checkbox question:

            {
              "type": "checkbox",
              "name": "question1"
              "title": "Please select all checkboxes to confirm eligability"
              "choicesByUrl": {
                "url": "/api/{someCustomVaraiable}",
                "allowEmptyResponse": true
              }
            }

And example validation expression would be: {question1.length} = {question1.choices.length}

question1.choices.lenght is just one option.

I've looked through the documentation but it seems all validation for checkboxes such as maxCount or minCount are not dynamic and one would need to know the number choices being present. If I'm missing something please point me in the right direction.

andrewtelnov commented 1 month ago

@gregpawlowski You can add a custom boolean property into checkbox class (can specify the category: "validation" to get it into the correct secition of the property grid) and then check if this property is true on survey,onLoadChoicesFromServer event, If it is true then set the checkbox.maxSelectedChoices property accordingly to your logic.

Thank you, Andrew

gregpawlowski commented 1 month ago

Thank you, I understand your solution.

I thought about this further and implemented a custom function to get the total number of choices then use that function in a validation expression. So that's another option.

Since there are two different ways to go about this I think this issue can be closed. I still think getting access to choice count in validation expressions or templates might be useful.

Thank you for you help, you're always so quick to respond and very helpful.