fix bug with forms where data and ui have different hierarchies

and add tests

fixes: http://manage.dimagi.com/default.asp?117610
This line reads pretty strangely...