1

I have a script tool in ArcGIS 10.1. Some of the parameters are optional and if they are left blank, will not be processed. They are numerical parameters and I want to enforce this in the tool, so an error is raised if entering any letters in the parameter for example. I have achieved this in updateMessages() setErrorMessage(...) and I get the correct error raised (see graphic) enter image description here

Validation code is as follows:

 @staticmethod
 def is_string(s):
 try:
   float(s)
   return False
 except ValueError:
   return True
 except TypeError:
   return True

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[1].clearMessage()

string_error = 'Please input a number, not text'
if ToolValidator.is_string(self.params[1].value):
  self.params[1].setErrorMessage(string_error)
if ToolValidator.is_string(self.params[2].value):
  self.params[2].setErrorMessage(string_error)
if ToolValidator.is_string(self.params[4].value):
  self.params[4].setErrorMessage(string_error)
if ToolValidator.is_string(self.params[5].value):
  self.params[5].setErrorMessage(string_error)

return

However, the "internal" ArcGIS validation seems to clear the field. If the tool is run the same error message will come up. However since I want to be able to have a blank field, this is not optimal as there is now no way of entering a blank value because the error keeps coming up. Is there any way of preventing ArcGIS validation from blanking out the field so that it retains the original value entered, which is more intuitive?

Also, I thought error messages were supposed to be red, not green - documentation for setErrorMessage() here.

5
  • 1
    Could you post your validation code? Could you just trap for None? Whatever your logic is plus "is not None" or something to that affect
    – KHibma
    Commented Dec 9, 2013 at 14:35
  • Thanks @KHibma. Have posted some code - I thought ArcGIS validation code was executed before user specified code though.
    – Stev_k
    Commented Dec 9, 2013 at 17:52
  • 1
    How exactly have you set up your parameters? Can't you just make them optional? I don't see why you need to use validation at all in this case.
    – blah238
    Commented Dec 9, 2013 at 18:27
  • 1
    I'm not a validation expert, but the indentation looks a little odd to me - the blocks of code underneath the def statements aren't indented. Is this copied directly from the validation script? Don't know if this is actually related.
    – thwllms
    Commented Dec 9, 2013 at 18:32
  • @bla238 - perhaps you're right - I just thought there would be a way to tell the user where they were going wrong - albeit it is fairly obvious!
    – Stev_k
    Commented Dec 9, 2013 at 20:32

2 Answers 2

3

If you set your parameter type to Double, any invalid entry is automatically converted to a 0 by the built-in validation. If you set it to Long, invalid entries are blanked out. Not sure why there is a difference in behavior, but if you can take advantage of it, it may be easier than trying to implement custom validation.

Number parameter validation

2

The green dot you're seeing says that's a required parameter and you haven't entered a value for it.

As for your question. I understand you want to validate a parameter to make sure that there is a NUMBER, not TEXT inside. This code below, based off yours does just that. Enter text, get an error. Enter a number, it passes. I hope this is what you're after.

  def updateMessages(self):
"""Modify the messages created by internal validation for each tool
parameter.  This method is called after internal validation."""

def is_string(s):
  try:
    float(s)
    return False
  except ValueError:
    return True
  except TypeError:
    return True    

self.params[0].clearMessage()
string_error = 'Please input a number, not text'

if self.params[0].value or self.params[0].altered:
  if is_string(self.params[0].value):
    self.params[0].setErrorMessage(string_error)   

return

Remember that the parameters are 0-indexed.

3
  • The parameter with a green dot is optional, which is why I am confused. It's not exactly what I'm after - I've already got validation to work as per the screenshot - it's more the blanking out of values in addition to this error message that I'm having issues with. Thanks though
    – Stev_k
    Commented Dec 9, 2013 at 20:31
  • You mean you wanted validation to clear a value? Maybe Im confused and didnt read that...that should just be a case of setting the value to ""
    – KHibma
    Commented Dec 9, 2013 at 20:39
  • Oh... now I think I understand what you're saying about clearing the field. Its probably exactly what blah has suggested, use a Long or Double (a number parameter type) vs. using a String input type. In my example, I've used a string input type. But if you only want numbers, using a Long or Double as the type is probably the best way to go.
    – KHibma
    Commented Dec 9, 2013 at 20:57

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.