Select to view content in your preferred language

Script Tool Validation based on field name

1545
7
Jump to solution
10-20-2021 08:35 AM
ZacharyHart
Honored Contributor

Proof of concept task here. I have had success enabling additional parameters if a dependent parameter met a certain qualification like so:

 

  def updateParameters(self):
    if self.params[5].value == "Supplemental":
        self.params[6].enabled = True
    else:
        self.params[6].enabled = False

 

Now I'd like to do something similar by evaluating the presence of an existing field in the first parameter (a feature layer). If the field is already present, then the following parameter is disabled:

 

  def updateParameters(self):
    if self.params[0].value:
        StandsFields = arcpy.Describe(self.params[0].value).fields
        for field in StandsFields:
            if field.name == "OS_TYPE":
                self.params[1].enabled = False
            else:
                self.params[1].enabled = True

 

However, the 2nd parameter is present no matter what.

Adapting this to a simple Python Window session works however:

 

StandsFields = arcpy.Describe(FC).fields
for field in StandsFields:
    if field.name == "OS_TYPE":
        print("True")
True

 

I've been searching around quite a bit here but no luck. Just barely getting into script tool validation. Any help is very much appreciated!

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

I think you are iterating over all fields and the last one's value is dictating if its false or true.  What you need to do is check if the name is in the list of field names:

 

 

if self.params[0].valueAsText:
        StandsFields = arcpy.Describe(self.params[0].valueAsText).fields
        if "OS_TYPE" in [f.name for f in StandsFields]:
            self.params[1].enabled = False
        else:
            self.params[1].enabled = True

 

or as

 

if self.params[0].valueAsText:
        StandsFields = arcpy.Describe(self.params[0].valueAsText).fields
        self.params[1].enabled = False if "OS_TYPE" in [f.name for f in StandsFields] else True

 

View solution in original post

7 Replies
by Anonymous User
Not applicable

try changing

self.params[0].value

to be ValueAsText.

if self.params[0].valueAsText:
        StandsFields = arcpy.Describe(self.params[0].valueAsText).fields
        for field in StandsFields:
            self.params[1].enabled = False if field.name == "OS_TYPE" else True

 

0 Kudos
ZacharyHart
Honored Contributor

Same behavior...

0 Kudos
by Anonymous User
Not applicable

I think you are iterating over all fields and the last one's value is dictating if its false or true.  What you need to do is check if the name is in the list of field names:

 

 

if self.params[0].valueAsText:
        StandsFields = arcpy.Describe(self.params[0].valueAsText).fields
        if "OS_TYPE" in [f.name for f in StandsFields]:
            self.params[1].enabled = False
        else:
            self.params[1].enabled = True

 

or as

 

if self.params[0].valueAsText:
        StandsFields = arcpy.Describe(self.params[0].valueAsText).fields
        self.params[1].enabled = False if "OS_TYPE" in [f.name for f in StandsFields] else True

 

ZacharyHart
Honored Contributor

Thanks Jeff, that's fixed it! any reason why it was working in the python command window?

 

0 Kudos
by Anonymous User
Not applicable

Nice!  It worked in the window because you isolated it in the conditional, skipping all other fields whose name was not "OS_TYPE".  If you had tested the field loop with the same if/else, you'd see each field's result printed and the last one in the iteration would be the value that the enabled variable would be left at.

ZacharyHart
Honored Contributor

I had to go back and stare at that until I understood. FWIW, I avoided using list fields in the tool validation as I was getting an IO error, found a few posts out there and opted for describe instead.

0 Kudos
by Anonymous User
Not applicable

I don't think we can avoid having lists here, even describe().fields is list of field objects.  But for fun we can condense it some more to result only one 'list' that will have the field or be empty, all in one line.

 

self.params[1].enabled = False if not [f.name for f in arcpy.Describe(self.params[0].valueAsText).fields if f.name == "OS_TYPE"] else True

 

 

0 Kudos