3

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:

  1. 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.
  2. But then when I choose the second layer from the TOC as param1 (it has no validation code) the param0 ́s red icon disappears like there is no error with the param0! I am sure that the problem is with if 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:

  1. 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
  2. (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?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jan 4, 2018 at 16:55

1 Answer 1

4

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!")
answered Jan 5, 2018 at 3:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.