5

I'm trying to make sure there is no '-' in the output filename, and if there is, to set an error (circle with the red 'X') so that the tool can't be run, and have a tooltip that explains the issue. I've tried a bunch of things, but from what I've read, it seems this should work:

if parameters[7].value:
            if parameters[7].value.value.count("-") > 0:
                parameters[7].setErrorMessage("Output cannot have a '-'." + \
                    "Please change the dash and rename the output after the tool has run")
            else:
                parameters[7].clearMessage()
        return

I'm not super clear on what is returned from the list of parameters. The paramater type is "DEShapefile". There is no error message being thrown when I use "--" as the filename.

Can someone see why it's not working for me?

2
  • You have .value.value in your second line, should that not be just .value?
    – Hornbydd
    Commented Nov 25, 2013 at 22:08
  • That's what I thought at first, but it didn't work at all. I even tried str(parameters[7].value).count(). No luck. I got .value.value from here. Near the bottom: resources.arcgis.com/en/help/main/10.1/index.html#/…
    – TSJ
    Commented Nov 25, 2013 at 22:15

1 Answer 1

7

I spoofed up a Python script of 1 parameter and wired it into toolbox and this is how I got it to check a FeatureClass input:

import arcpy

class ToolValidator(object):
  """Class for validating a tool's parameter values and controlling
  the behavior of the tool's dialog."""

  def __init__(self):
    """Setup arcpy and the list of tool parameters."""
    self.params = arcpy.GetParameterInfo()

  def initializeParameters(self):
    """Refine the properties of a tool's parameters.  This method is
    called when the tool is opened."""
    return

  def updateParameters(self):
    """Modify the values and properties of parameters before internal
    validation is performed.  This method is called whenever a parameter
    has been changed."""

    return

  def updateMessages(self):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""
    self.params[0].clearMessage()
    if self.params[0].value is None:
        self.params[0].clearMessage()
    else:
        fc = str(self.params[0].value.value)
        if fc.count("-") > 0:
            self.params[0].setErrorMessage("Output cannot have a dash")
        else:
            self.params[0].clearMessage()
    return
2
  • 2
    I don't think you need to call clearMessage() twice.
    – blah238
    Commented Nov 25, 2013 at 23:27
  • Hmm... I'll try putting my code in updateMessages(), I had it in updateParameters(). It must be that any messages I am trying to show are being overwritten by the validating step. I'll try it out first chance I get. Thanks!
    – TSJ
    Commented Nov 26, 2013 at 17:11

Your Answer

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

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