Views Numeric Range Validator

Categories

Component ID

2634012

Component name

Views Numeric Range Validator

Component type

module

Maintenance status

Development status

Component security advisory coverage

not-covered

Component created

Component changed

Component body

Adds a Views contextual filter validator for numbers in a configurable range.

Adding a numeric range validator allows you to control what happens when numbers entered into the argument (normally a path) fall outside the specified range. Normally, the Numeric validator will allow any numeric path argument to be passed into a view.

Why is this useful?

A common use cases for limiting the argument to a range can be Date based arguments. Limit Month or Day values and provide a "Page not found" response if arguments are entered outside of a normal range (e.g. Month 1-12, Day 1-31).

For example, you use a contextual filter to set up a Blog overview page that displays all posts created in a certain Year. You create a view to handle this overview page and add a "Content: Created year" contextual filter so that you can use a clean path like "/blog/2015" to display the overview. Without this extra validator, you select the core Numeric validator and even select "Show Page not found" for "Action to take if filter value does not validate". With this configuration, paths like /blog/1500 and even /blog/-100 are valid paths since both 1500 and -100 are numeric. Even though they will not likely show content, the No Results Behavior is displayed (with a 200 response header). Responding with a Page Not Found (404) would be a much better response to non-sensical URL requests.

Alternatives

In Drupal 7, it was possible to enter custom php validator code into the view to provide this functionality. This option is not provided in Drupal 8 Core, and is not the preferable way to add argument validation.

To use:

  1. Enable Views Numeric Range Validator module in /admin/modules
  2. Add any number based contextual filter to a view. Contextual filters are found under the "Advanced" section in the Views UI.
  3. In the Contextual Filter configuration, enable the checkbox "Specify Validation Criteria" under "When the Filter Value is in the URL or a Default is Provided".
  4. Select "Numeric in Range" in the Validator select box.
  5. Specify a minimum and maximum value for the range.