Fixes for #5252

Fixes for #5252 and refactoring. Perhaps you need to fix js validation too. I did not watch it.
I don't think this check is correct. Imagine if `$value` is 0.