We will face problem in a case when PhoneNumber field will be empty. User will try to change JobId field value to something else, but he will be stuck because framework will report required field validation error:
This happens because there was defined PartialTrigger dependency between PhoneNumber and JobId. Any change in parent attribute triggers child attribute, where validation is invoked and failed for empty value.
Instead of defining PartialTrigger dependency directly between two fields, we should invoke it through partial trigger dependency in Backing Bean code. Let's define ValueChangeListener for parent field, JobId in my case:
Listener code is self explanatory, based on JobId value, we are setting PhoneNumber as required or not and then invoking partial target. Its a key difference comparing to PartialTrigger dependency, now we are changing dependent attribute required property first and only then triggering this attribute refresh. This allows to avoid validation error while changing condition for required property:
It works fine, but you shouldn't forget initial PhoneNumber correct rendering, there is already predefined expression for required option. This creates trouble, because we are trying to change it from value change listener, ADF_FACES-60058 exception is thrown while changing JobId:
Its because exists already predefined expression language for Required property:
Now we will apply old trick from ADF 10g, instead of using expression language, will calculate Required property value in Backing Bean method:
This method works in the same way as value change listener explained above, only one difference - it always returns false:
Main trick - you should define this method not directly on Required property itself, but on unrelated property, let's say - Readonly. This allows to avoid ADF_FACES-60058 described above. Its why we are returning always false, because field should be always editable.
On runtime, when JobId is not AD_PRES, PhoneNumber is not required:
If we navigate to such record, where JobId is AD_PRES, PhoneNumber will be automatically required:
Find employee with JobId not equal AD_PRES and change it to AD_PRES:
PhoneNumber field will become required:
Now try to change JobId again, you should be allowed to do the change:
PhoneNumber field will become not required:
Download sample application - ConditionalMandatory.zip.