Skip to main content

Adding custom validation to grid questions

Or the art of making sure respondents are paying attention

Updated over 4 months ago

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, you pass a dictionary of responses—one for each row in the grid.

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:

1. Accepts the responses submitted by the user.

2. Returns a string to be displayed to the user if validation fails.

• If the string is empty, 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"],
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. Here’s how you might implement this:

s.grid_select_question(
"Please rate the following aspects of the product",
rows=["Quality", "Price", "Service", "Delivery"],
options=["Poor", "Fair", "Good", "Very Good", "Excellent"],
custom_validator=lambda response: (
"Please select at least three different options"
if len(set(response.values())) < 3
else ""
)
)

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"],
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 ""
)
)

How It Works

  1. The custom_validator function checks the response dictionary: It counts how many rows are marked as “My least favorite” and “My favorite.”

  2. If either count exceeds one, the function returns an error message prompting the user to adjust their selections.

  3. If both counts are valid (i.e., exactly one for each), the function returns an empty string, allowing the response to proceed.

This ensures the question’s constraints are respected while providing clear feedback to the respondent.

Did this answer your question?