You can provide custom behavior for your script tool dialog box, such as enabling and disabling parameters, providing default values, and updating string keywords. By adding Python code, you can do the following:
- Enable or disable a parameter based on values contained in other parameters.
- Update a parameter filter. Using a field filter, you can create a list of valid field types, such as Long and Double. With a string filter, you can set a list of valid keywords, as shown below. There are six types of filters: Value List, Range, Feature Class, File, Field, and Workspace.
- Provide default values for parameters, such as cell size for rasters.
- Customize warning and error messages that appear on the dialog box.
- Put parameters in different categories.
- Update the description of output datasets for use in ModelBuilder.
How validation works
Validation is performed with a block of Python code that geoprocessing uses to control how the tool dialog box and Python window change based on user input. System tools (those provided by Esri) can react to user input and subsequently modify the tool dialog box as described above.
Validation means checking that all tool parameters are correct and providing useful messages if they are not. There are two parts to validation:
- The code you add.
- The automatic internal validation (or basic validation) performed by geoprocessing in ArcGIS.
Internal validation does the following:
- If a parameter is required, it checks whether it's empty (nothing entered) and, if so, indicates that the value is required on the tool dialog box (using a green dot instead of a red X).
- Checks that the value entered is of the correct type (for example, a raster instead of a feature class, or alphabet characters instead of a number).
- Checks filter membership. That is, if you have a Value List filter containing keywords such as RED, ORANGE, and YELLOW, and you enter BLUE, you'll receive an error message, because BLUE isn't in the Value List filter.
- Checks the existence of input datasets.
- Generates a default catalog path for output datasets.
- Updates the description of the output data based on a set of rules contained in the special object, Schema.
- Checks the existence of an output dataset with the overwrite output geoprocessing option. If the dataset exists and the option is false, an error occurs; otherwise, a warning appears.
- If the parameter is a Field data type, it confirms that the field exists in the associated table.
- Checks that the output dataset isn't the same as the input dataset (unless the output is derived, such as with Add Field).
- For parameters containing linear and areal unit data types, it sets their default values by examining the corresponding values in ArcMap (if it's running in ArcMap).
- If the output is a coverage, grid, or INFO table, it checks the 13-character file name limit for these datasets.
Internal validation doesn't do the following (but you can with your own validation code):
- Update filters based on interaction with other parameters. For example, if your user enters a point feature class in the first parameter, you want your tool dialog box to display RED, ORANGE, and YELLOW in the third parameter. If a polygon feature class is entered, you want to display BLUE, INDIGO, and VIOLET in the third parameter.
- Enable or disable parameters.
- Calculate default values.
- Perform any tool-specific parameter interaction.
The code you add works with internal validation as follows:
- You can provide a set of rules that internal validation will use to update the description of output datasets. These rules are contained in a Schema object.
- You can update filters before internal validation occurs. Using the example above, if a point feature class is entered, update the filter to contain RED, ORANGE, and YELLOW. Internal validation checks the value entered with the values in the filter.
And, as mentioned, you can have your validation code calculate default values, enable and disable parameters, and customize messages. These types of actions have no consequence for internal validation; they only affect the appearance of the tool dialog box.