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)
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.