Conditionally require subform fields using Client Script

Conditionally require subform fields using Client Script

I have three subform fields that I would like to make required based on a response in another field. I have set up a layout rule to hide the subform when not needed, but it does not hide it on the Edit page so setting the fields to be required on the layout properties is useless as it make is always required. I have been able to access the subform fields to get and set the values using a client script, but is it possible to use the setMandatory function? I have not been able to get it to work.