How to validate readonly fields

Marking field as required doesn’t seem to be enough! It aslo needs to be mandatory in database which is VERY WRONG. What if I want a field which is not mandatory but required. It is hidden until user ticks a tickbox. When user ticks tickbox it becomes visible and it mustn’t be allowed to commit a screen with such a field. However a validator isn’t called for it.
(1)it gets red and message is shown!
(2)despite (1) screen is commited!
How do I prevent such screen (with required but not mandatory field) from commiting ?

Hi,

There is probably a better way to do it, but if we are speaking about Edit screen you can look into preCommit or postValidate hooks.