I am writing a tool within Python Toolbox that has two parameters:
param0 = arcpy.Parameter(
name="a",
displayName="A",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input")
param1 = arcpy.Parameter(
name="b",
displayName="B",
datatype="GPFeatureLayer",
parameterType="Required",
direction="Input")
params = [param0, param1]
I can ́t figure out which if
statement I should use in def updateMessages(self, parameters):
. Here is a step-by-step what ́s going wrong when I run the tool:
- I choose a layer from the TOC as
param0
. My validation code is
if not parameters[0].hasBeenValidated: ... check e.g. if param0 has some attribute field ... ... it turns out that such field doesn ́t exist, so... parameters[0].setErrorMessage("No field!")
The red error icon appears next to its label with the message "No field!" - everything is working as expected. - But then when I choose the second layer from the TOC as
param1
(it has no validation code) theparam0
́s red icon disappears like there is no error with theparam0
! I am sure that the problem is withif not parameters[0].hasBeenValidated:
as I don ́t quite understand how it works. I tested several code variants with.hasBeenValidated
,.altered
etc. but with no success.
To be short:
- I would need the red error icon to be displayed as long as there is an error with the
param0
no matter what I do with other parameters and - (if possible - not mentioned above) ensure that
param0
́s validation performs only when the user changes the value of this parameter.
Could you give me a hint how to accomplish this?
1 Answer 1
If you use altered
method of the Parameter
object, your simple validation routine should work fine, e.g.,
def updateMessages(self):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
if parameters[0].altered:
#check e.g. if param0 has some attribute field ...
#... it turns out that such field doesn ́t exist, so...
if not "Sought_Field" in [f.name for f in arcpy.ListFields(parameters[0].value)]:
parameters[0].setErrorMessage("No such field!")
Explore related questions
See similar questions with these tags.