You may already know our article on adding custom validation to questions. When it comes to grid questions, validation offers more flexibility. Instead of passing a single response to the custom_validator, the platform passes a dictionary of responses - one for each row in the grid. (In the chat experience, where the grid is asked one row at a time, the validator is instead called with each row's individual response.)
By default, the validator checks for straight-liners, i.e., respondents who select the same option across all rows.
Customizing the Validator
Suppose you want to ensure that respondents select at least three distinct options. In that case, you'll need to write a custom validator.
The validator is a function that:
- Accepts the responses submitted by the user.
- Returns a string to be displayed to the user if validation fails.
- If the function returns
None, the response is considered valid.
Example Scenario
Consider the following question:
s.grid_select_question(
"Please rate the following aspects of the product",
rows=["Quality", "Price", "Service", "Delivery"],
row_name="Product Aspect",
options=["Poor", "Fair", "Good", "Very Good", "Excellent"]
)
If a respondent selects "Poor" for all aspects except "Service," which they rate as "Good," the response passed to the validator would look like this:
{
"Quality": "Poor",
"Price": "Poor",
"Service": "Good",
"Delivery": "Poor"
}
You can check the number of unique values in the responses to validate that the respondent has selected at least three distinct options. The sandbox does not allow the set() builtin, so use dict.fromkeys() to count distinct values instead:
s.grid_select_question(
"Please rate the following aspects of the product",
rows=["Quality", "Price", "Service", "Delivery"],
row_name="Product Aspect",
options=["Poor", "Fair", "Good", "Very Good", "Excellent"],
custom_validator=lambda response: (
"Please select at least three different options"
if len(dict.fromkeys(response.values())) < 3
else None
)
)
This approach ensures more nuanced validation, helping you maintain data quality while tailoring the survey experience to your needs.
Restricting the number of people who choose each option
If you have a grid question with specific constraints - for example, allowing only one option to be selected as "My least favorite" and one as "My favorite" - you can enforce this with a custom validator.
Here's an example:
s.grid_select_question(
"How much do you like each fruit?",
rows=["Bananas", "Oranges", "Apples"],
row_name="Fruit",
options=["My least favorite", "Don't like", "Like", "My favorite"],
custom_validator=lambda response: (
"Please select only one option as 'My least favorite' and one as 'My favorite'"
if list(response.values()).count("My least favorite") > 1
or list(response.values()).count("My favorite") > 1
else None
)
)
How It Works
- The custom_validator function checks the response dictionary: It counts how many rows are marked as "My least favorite" and "My favorite."
- If either count exceeds one, the function returns an error message prompting the user to adjust their selections.
- If both counts are valid (i.e., exactly one for each), the function returns
None, allowing the response to proceed.
This ensures the question's constraints are respected while providing clear feedback to the respondent.

