I haven't used the Tool Validator, but I have done tool validation with Python Toolbox (.pyt) files, and the syntax is similar, so maybe this help. In a tool that I have I am checking the same thing with the following code:
if parameters[1].valueAsText == parameters[2].valueAsText:
parameters[1].setErrorMessage("FIELD1 value cannot be the same as FIELD2 value.")
parameters[2].setErrorMessage("FIELD2 value cannot be the same as FIELD1 value.")
You do not need to check if both fields have been set first. Also, you want to check the "Value As Text", which will return a the name of the field specified. I think that paramter.value would return a Field Object.So I would think that the following would work for you:
if self.params[3].valueAsText == self.params[4].valueAsText:
self.params[3].setErrorMessage("Cannot pick the same field")
self.params[4].setErrorMessage("Cannot pick the same field")
But like I said, I haven't done exactly what you are trying. But since the syntax for all of this is generally poorly documented, a lot of what I have learned to do with pyt files has come from copying/modifying the help info for the tool validator box syntax.Good luck,- RussellPS ... and this is a stretch... but at least with a pyt file you can dynamically update the fields such that it is impossible for the two fields to be the same. For example, the code block below makes sure that whatever fields a user specifies as input for two early parameters cannot be used as input for a third parameter (and keeps the list of options up to date if anything changes). This would go in the update parameters section. Again the syntax is for pyt files.
def updateParameters(self, parameters):
'''Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed.'''
#Need to make sure that the additional fields option ONLY lists fields
#that will not be standardized.
if parameters[0].value:
old_fields = [field.name for field in arcpy.ListFields(parameters[0].value)]
if parameters[1].altered:
if parameters[1].valueAsText in old_fields:
old_fields.pop(old_fields.index(parameters[1].valueAsText))
if parameters[2].altered:
if parameters[2].valueAsText in old_fields:
old_fields.pop(old_fields.index(parameters[2].valueAsText))
parameters[3].filter.list = old_fields
The above sets the 4th parameter value options to be a list of fields from the input to the 1st parameter. However, any values selected in the 2nd and 3rd parameters will be removed from the list of options for the 4th parameter.