Conditional Drop Down Lists - Tool Validator

3352
17
Jump to solution
03-07-2018 05:23 PM
AmandaMacDonald-Creevey
New Contributor II

I'm trying to use the ToolValidator function to update the content of the drop down lists. I've followed the steps from https://blogs.esri.com/esri/arcgis/2011/08/25/generating-a-choice-list-from-a-field/  but need to take it a step further and populate the 'HA' drop down list with the values in the 'HarvestArea' field that start with the 'ForestID'. 

Any help on how to format this in the ToolValidator script (see below) would be much appreciated! 

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()
    self.fcfield = (None, None)

  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 parmater
    has been changed."""

    if self.params[0].value and self.params[1].value:
      fc, col = str(self.params[0].value), str(self.params[1].value)
      if self.fcfield != (fc, col):
        self.fcfield = (fc, col)
        self.params[2].filter.list = [str(val) for val in
                                        sorted(
                                          set(
                                            row.getValue(col)
                                            for row in arcpy.SearchCursor(fc,
                                                                          fields=col)
                                              )
                                           )
                                      ]
        if self.params[2].value not in self.params[2].filter.list:
          self.params[2].value = self.params[2].filter.list[0]

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

    return
17 Replies
MichaelMichaelides
New Contributor II

Mr Burton i didn't know how else to contact you regarding a similar issue that i have. I have posted my issue in the following url: https://community.esri.com/thread/213736-select-by-many-attributes-and-then-zoom-to-selected .

I tried searching to find a solution but until today i haven't found or given a solution that actually works. If you could find the time and take a look at my request i would appreciate it. 

I already tried to alter the Tool validator with my columns based on the solution provided in your answer and added the new script tool (without changing anything) also from your answer within the current toolbox that i had but didn't work.

Please i need your guidance.  

Regards 

Mike

0 Kudos
RobertKnapik
New Contributor III

Your code is very useful. Is it possible to publish this script tool as a web tool (in Portal for ArcGIS) and use it in geoprocessing widget in Web AppBuilder? I tried but only one value appears on the lists. Maybe I make a mistake somewhere...

Thanks,

Robert

0 Kudos
RandyBurton
MVP Regular Contributor

I haven't worked with Portal, so I'm not sure how it would work as a web tool.  I suggest that you post a new question and include a link to this one for reference.  Can you provide a bit more about what you want your tool to do?

0 Kudos
RobertKnapik
New Contributor III

I would like my tool to work like yours but with different data. I made a scripting tool in ArcGIS Pro and it works. Now I would like to embed the tool in a web application using geoprocessing widget. I found out that this is not possible because script validation doesn't work in geoprocessin widget. Look at this: How to Implement Custom tool Validation for Geoprocessing Widget. I this case, probably the only solution is to create a custom widget using java script.

JyotiRohodia
New Contributor II

Same Issue. You got any workaround?

0 Kudos
RobertKnapik
New Contributor III

Unfortunately I don't have a simple solution. It seems to me that you need to download the "geoprocessing" widget and modify it using java script. Then you need to place the custom widget on the webserver.

JyotiRohodia
New Contributor II

Thanks for the reply!

Do you happen to know any specific library that can help implement validation for fields. I had some experience with WAB Dev edition in the past but I am No Pro.

0 Kudos
RobertKnapik
New Contributor III

I don't have experience with java script and WAB Dev edition, but I would love to . Maybe in the future... Let me know if you find any solution.