I'm currently acquainting myself with the parameter and parameter validation methods of the tool class and I can't quite figure out what's required to get error/warning messages to properly populate and how updateParameters and updateMessages differ for setErrorMessage/setWarningMessage. Either I get no message, the message doesn't update on value change, or its the wrong one. What is the correct code to make sure that a parameter is properly validated after it changes and then displays the proper message (if any)?
For a simple example, I just want to validate that a given parameter is in a list of strings:
project_ids = ['123', '456', '789']
class tool:
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = 'test tool'
self.description = "test"
def getParameterInfo(self) -> list:
"""Define the tool parameters."""
project_id = arcpy.Parameter(name="project ID",
displayName="project ID",
datatype="GPString",
parameterType="Required", # Required|Optional|Derived
direction="Input", # Input|Output
)
return [project_id]
def isLicensed(self):
"""Set whether the tool is licensed to execute."""
return True
def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
if parameters[0].altered:
if parameters[0].value is None:
parameters[0].setErrorMessage("No Project Specified")
elif parameters[0].valueAsText not in project_ids:
parameters[0].setErrorMessage('Unrecognized project ID')
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
# The only time I got messages to show up was when I put the code block from updateParameters here
return
@staticmethod
def execute(self, parameters, messages=None):
"""The source code of the tool."""
project_id = parameters[0]
test_function(project_id)
return
def postExecute(self, parameters):
"""This method takes place after outputs are processed and
added to the display."""
return