Validation: Comparing two fields

Keep a user from requesting a meaningless change request.
Written by Nick Laughton
Updated 1 year ago

It is common in forms to have a request to change a value from one value to another. But without form validation, it would be easy to accidentally submit requests where the values are the same. We can prevent that by using field validation rules.  Let's look at a request to change a Product Type.

In this example we have two Select options. Because the form could potentially be changing from any product type, both have the same values available.  The two fields are bound to existingProductType and newProductType

The obvious thing would be to change the second field's Invalid If property to the binding of the first.  Like this:

This works, but it creates an interesting (annoying) side effect. When the page loads, both fields would have the same value (null) and therefore the page would always start with a validation error being shown.

This is not an optimal outcome.  To address it, we need to check for two conditions.  Are the two values the same, and does the first field actually have a value?

To check two validation conditions, we need to wrap them both in parenthesis and decide if we need an and or an or between them.  In this case we need an and
because we need both conditions to be true for an error condition.

(newProductType) and (newProductType is existingProductType)

This lets our page load without an error, yet also stops users from accidentally wasting time by submitting a pointless request.

Now, with this code in place the page loads without an error condition:

And if we select the same value for both fields, we get the warning we need for users.

Did this answer your question?