2

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?

2

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 field!")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.